首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在docker容器中使用numpy运行python代码时,即使在dockerfile /requirements.txt中添加了numpy,也会出现“模块找不到错误”

在docker容器中使用numpy运行python代码时,即使在dockerfile /requirements.txt中添加了numpy,也会出现“模块找不到错误”
EN

Stack Overflow用户
提问于 2020-02-18 22:40:41
回答 2查看 2.1K关注 0票数 0

我在一个dev容器中运行一些非常基本的python代码。

我想让它和numpy一起工作

在我尝试让它与numpy一起工作之前,一切都运行得很完美。

我在我的python代码中写了一行:import numpy as np

以下是我在容器中安装numpy的步骤:

我在dockerfile中为numpy添加了pip install:pip install numpy==1.14.3 (带和不带版本...)我得到了这个错误:

代码语言:javascript
复制
import numpy as np
ModuleNotFoundError: No module named 'numpy'

我尝试在requirements.txt中添加numpy,在dockerfile文件中添加COPY requirements.txt /tmp/pip-tmp/

这是我的Dockerfile:

代码语言:javascript
复制
FROM python:3

# Avoid warnings by switching to noninteractive
ENV DEBIAN_FRONTEND=noninteractive

# This Dockerfile adds a non-root user with sudo access. Use the "remoteUser"
# property in devcontainer.json to use it. On Linux, the container user's GID/UIDs
# will be updated to match your local UID/GID (when using the dockerFile property).
# See https://aka.ms/vscode-remote/containers/non-root-user for details.
ARG USERNAME=vscode
ARG USER_UID=1000
ARG USER_GID=$USER_UID

# Uncomment the following COPY line and the corresponding lines in the `RUN` command if you wish to
# include your requirements in the image itself. It is suggested that you only do this if your
# requirements rarely (if ever) change.
COPY requirements.txt /tmp/pip-tmp/

# Configure apt and install packages
RUN apt-get update \
    && pip install numpy==1.14.3 \
    && apt-get -y install --no-install-recommends apt-utils dialog 2>&1 \
    #
    # Verify git, process tools, lsb-release (common in install instructions for CLIs) installed
    && apt-get -y install git openssh-client iproute2 procps lsb-release \
    #
    # Install pylint
    && apt-get -y install libc-dev \
    && apt-get -y install build-essential \
    && pip install -U pip \
    && pip --disable-pip-version-check --no-cache-dir install pylint \
    #&& pip install --no-cache-dir numpy scipy pandas matplotlib \
    #
    # Update Python environment based on requirements.txt
    && pip --disable-pip-version-check --no-cache-dir install -r /tmp/pip-tmp/requirements.txt \
    && rm -rf /tmp/pip-tmp \
    #
    # Create a non-root user to use if preferred - see https://aka.ms/vscode-remote/containers/non-root-user.
    && groupadd --gid $USER_GID $USERNAME \
    && useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME \
    # [Optional] Add sudo support for the non-root user
    && apt-get install -y sudo \
    && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME\
    && chmod 0440 /etc/sudoers.d/$USERNAME \
    #
    # Clean up
    && apt-get autoremove -y \
    && apt-get clean -y \
    && rm -rf /var/lib/apt/lists/*

# Switch back to dialog for any ad-hoc use of apt-get
ENV DEBIAN_FRONTEND=dialog

我得到了同样的错误

(我构建了我的容器,并在每一步之后重新运行它)

如果您知道如何帮助我修复它,请让我知道

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-27 05:31:35

为了确保您没有使用不同的python环境,可以通过安装python3-virtualenv在您的容器中创建一个python虚拟环境。并使用下面的命令激活它。

代码语言:javascript
复制
RUN python3 -m virtualenv --python=/usr/bin/python3 /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

如果问题仍然存在,请尝试手动删除容器镜像并重新创建它。

票数 0
EN

Stack Overflow用户

发布于 2020-02-18 23:00:53

一种理论是:您意外地安装了Debian打包的Python。因此,现在您有两个Python,一个来自Docker映像(在其中进行pip安装),另一个在命令行上运行,这是不同的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60283366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档