pycharm doesn't recognize module when interpreter running on docker
I've got a simple dockerfile:
FROM python:stretch
RUN pip install <packages>
I build it, then set pycharm to use it as an interpreter, following these instructions.
It seems to work, until I go to do something like:
from my_module import <some_packages>
I get:
ModuleNotFoundError: No module named 'my_module'
Now, when I go and `docker exec -it <my_container> /bin/bash` into my container, I can `cd` into the instances working directory and manually run python (or ipython), and import the module.
But it doesn't work in pycharm.
The output of os.environ['PYTHONPATH'].split(os.pathsep)` is identical between the `docker exec` command line and pycharm's embedded terminal:
I'd appreciate some help diagnosing and fixing this problem as it appears to be fairly pycharm-specific!
Please sign in to leave a comment.
Hi,
Unfortunately it's not enough to see where is the issue. According to your description you're doing everything correctly.
Please provide screenshots/outputs of the following:
1. `pip list` and `which python` from inside the container
2. Screenshots of your run/debug configuration in PyCharm
3. Full output of the error, including the first line (where the executing command is shown.)
Thanks for following up. I made a MWE. Here's the dockerfile:
FROM python:stretch
RUN pip install pandas
Here's `my_module.py`
Here's `my_script.py`
I put all of these things in a dir and run
Then I add the remote interpreter per the instructions:
Then I try to run it from pycharm
Then I exec into the container with `docker exec -it <container name> /bin/bash and run `python my_script`. I get
So it works
Here is which python and pip list:
/usr/local/bin/python
Package Version
--------------- -------
numpy 1.17.4
pandas 0.25.3
pip 19.2.3
python-dateutil 2.8.1
pytz 2019.3
setuptools 41.2.0
six 1.13.0
wheel 0.33.6
Working fine from my side following your steps. You did not post the full output of the run console showing where the script is being run from in the container.
Make sure the location where it's run from is consistent with volume bindings in your run/debug configuration, i.e. in my case the bindings are:
-v /home/andrew/projects/pycharm/docker_test:/opt/project
And the run command is:
daba94c85f42:python -u /opt/project/my_script.py
Hi.
I habe a similar issue here with PyCharm and Docker on Windows. I've a docker image with Python 3.9 and I've installed two modules there with pip. These are listed on the Docker container with pip list. When I add a new Python interpreter and choose the docker image, the Python version is retrieved correctly but the extra modules are missing. When running the script the modules are not found too.
Can anybody help me out, because theses modules don't work on Windows.
Thx in advance
Steven Woelk
Did you install your additional modules using Dockerfile? Please note that you should add your `pip install` instructions to the Dockerfile and rebuild the image, otherwise all changes are wiped when you restart the container.