python - 即使在dockerfile/requirements.txt中添加了numpy之后,在docker容器中使用numpy运行python代码的“Module not found error”

标签 python docker numpy module dockerfile

我正在从开发容器中运行一些非常基本的python代码

我想让它与numpy一起使用

在我尝试使其与numpy一起使用之前,evrything可以正常工作。

我在python代码中写了这一行:import numpy as np
这些是我在容器中安装numpy的步骤:

我在dockerfile中为numpy添加了pip安装:
pip install numpy==1.14.3(带有和不带有版本...)
我收到此错误:

import numpy as np
ModuleNotFoundError: No module named 'numpy'

我尝试在dockerfile中的requirements.txt和COPY requirements.txt /tmp/pip-tmp/中添加numpy

这是我的Dockerfile:
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

我有同样的错误

(我建立了容器,并在每个步骤之后重新运行它)

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

最佳答案

为确保您未使用其他python环境,请通过安装python3-virtualenv在您的容器中创建python虚拟环境。并使用以下命令将其激活。

RUN python3 -m virtualenv --python=/usr/bin/python3 /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

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

关于python - 即使在dockerfile/requirements.txt中添加了numpy之后,在docker容器中使用numpy运行python代码的“Module not found error”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60283366/

相关文章:

docker - 如何确认在 docker 容器中进行的安装

selenium - "ChromeHeadless have not captured in 60000 ms, killing."仅发生在 Gitlab 托管的 CI/CD 管道中

docker - 如何将 Docker Desktop Kubernetes 集群迁移到 Google Kubernetes Engine

python - 在 Python/Numpy 中实现具有不同系数的简单滤波器的有效方法

python - 无法确定 SyntaxError : invalid character in identifier 的原因

.net - 等待单个 RabbitMQ 消息超时

python - 检查一个字节是否可以 ascii 打印

python - numpy.resize 和 numpy.reshape 函数如何在 python 内部工作?

python - 与复数相关的点积

python - 使用索引遍历 numpy(相当于 python 枚举的 numpy)