简体   繁体   中英

"Module not found error" on running python code with numpy in docker container even after adding numpy in dockerfile /requirements.txt

I am running from a dev container some very basic python code

I wanted to make it work with numpy

Before I tried to make it work with numpy , evrything worked perfectly.

I wrote in my python code this line : import numpy as np

These are the steps I followed to install numpy inside my container:

I added pip install for numpy in the dockerfile : pip install numpy==1.14.3 (with and without versions ...) I got this error :

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

I tried adding numpy in the requirements.txt and COPY requirements.txt /tmp/pip-tmp/ in the dockerfile

This is my 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

I got the same error

(I built my container and rerun it after each step )

If you know how to help me fixing it please let me know

One theory: you're accidentally installing the Python packaged by Debian. So now you have two Pythons, the one from the Docker image (where you're pip installing) and the one you run on the command-line, which is different.

To make sure you are not using different python environments, create a python virtual environment in your container by installing python3-virtualenv . And using the below command to activate it.

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

If the problem still persists, try manually removing the container image and recreating it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM