“ModuleNotFoundError: No module named <package>” in my Docker container

匿名 (未验证) 提交于 2019-12-03 00:59:01

问题:

I'm trying to run a python script in a Docker container, and i don't know why, python can't find any of the python's module. I thaught it has something to do with the PYTHONPATH env variable, so i tried to add it in the Dockerfile like this : ENV PYTHONPATH $PYTHONPATH

But it didn't work. this is what my Dockerfile looks like:

FROM ubuntu:16.04 MAINTAINER SaveMe SaveMe@Desperate.com  ADD . /app WORKDIR /app  RUN apt-get update  RUN DEBIAN_FRONTEND=noninteractive apt-get install -y locales  # Set the locale RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/'  /etc/locale.gen && \     locale-gen ENV LANG en_US.UTF-8 ENV LANGUAGE en_US:en ENV LC_ALL en_US.UTF-8 ENV PYTHONPATH ./app  #Install dependencies RUN echo "===> Installing sudo to emulate normal OS behavior..." RUN apt-get install -y software-properties-common RUN apt-add-repository universe RUN add-apt-repository ppa:jonathonf/python-3.6 RUN (apt-get update && apt-get upgrade -y -q && apt-get dist-upgrade - y -q && apt-get -y -q autoclean && apt-get -y -q autoremove) RUN apt-get install -y libxml2-dev libxslt-dev RUN apt-get install -y python3.6 python3.6-dev python3.6-venv openssl  ca-certificates python3-pip RUN apt-get install -y  python3-dev python-dev libffi-dev gfortran RUN apt-get install -y swig RUN apt-get install -y sshpass openssh-client rsync python-pip python- dev libffi-dev libssl-dev libxml2-dev libxslt1-dev libjpeg8-dev  zlib1g-dev libpulse-dev RUN pip install --upgrade pip RUN pip install bugsnag  #Install python package + requirements.txt RUN  pip3 install  -r requirements.txt   CMD ["python3.6", "import_emails.py"] 

when i'm trying to run: sudo docker run <my_container> i got this Traceback:

Traceback (most recent call last):   File "import_emails.py", line 9, in <module>     import bugsnag ModuleNotFoundError: No module named 'bugsnag' 

As you can see i'm using python3.6 for this project. Any lead on how to solve this ?

回答1:

Inside the container, when I pip install bugsnag, I get the following:

root@af08af24a458:/app# pip install bugsnag Requirement already satisfied: bugsnag in /usr/local/lib/python2.7/dist-packages Requirement already satisfied: webob in /usr/local/lib/python2.7/dist-packages (from bugsnag) Requirement already satisfied: six<2,>=1.9 in /usr/local/lib/python2.7/dist-packages (from bugsnag) 

You probably see the problem here. You're installing the package for python2.7, which is the OS default, instead of python3.6, which is what you're trying to use.

Check out this answer for help resolving this issue: "ModuleNotFoundError: No module named <package>" in my Docker container

Alternatively, this is a problem virtualenv and similar tools are meant to solve, you could look into that as well.



回答2:

since you're using py3, try using pip3 to install bugsnag instead of pip



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!