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!

 

2
5 comments

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.)

0
Avatar
Permanently deleted user

Thanks for following up.  I made a MWE.  Here's the dockerfile:

FROM python:stretch
RUN pip install pandas

Here's `my_module.py`

def cool_function():
print("this function is rad")

Here's `my_script.py`


from my_module import cool_function
print("woo")

I put all of these things in a dir and run

docker build -t test_image .

Then I add the remote interpreter per the instructions:

Then I try to run it from pycharm

 

from my_module import cool_function
Traceback (most recent call last):
File "<input>", line 1, in <module>
ModuleNotFoundError: No module named 'my_module'

Then I exec into the container with `docker exec -it <container name> /bin/bash and run `python my_script`.  I get

 

$ docker exec -it 10bb4072d879 /bin/bash
root@10bb4072d879:/opt/project# ls
dockerfile my_module.py script.py
root@10bb4072d879:/opt/project# python
Python 3.7.4 (default, Sep 12 2019, 16:02:06)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
root@10bb4072d879:/opt/project# python script.py
woo



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

0

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

 

1

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

0

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.

0

Please sign in to leave a comment.