DOCKER container: /opt/project missing PYTHONPATH

Hello, I am new to docker and I have some difficulties trying to interface pycharm properly with my docker container.

As I seem to understand, pycharm mounts my project directory to /opt/project and it runs the scripts from said directory. 

I have a problem: even if a user defined library is present in my project directory (MyLib.py, for example), it is not recognized by the interpreter in the PythonConsole. I've figured out that this may be due to "/opt/project" not being present in the PYTHONPATH env variable. I've edited my dockerfile and added said folder to the PYTHONPATH variable, but apparently pycharm overwrites that env variable upon the automatic creation of the container, which makes my efforts useless...

A workaround is to start a PythonConsole manually and add /opt/project to path (with sys.path.insert, for example) or to edit the run config to point to the right file (pycharm apparently doesn't correctly choose /opt/project/test.py and tries to run it in D:/.../local/folder/test.py) and the right working directory (/opt/project)... Which is tedious! 

Is there a way to fix this problem? Did I configure something wrong?

Thank you for your help!

1
7 comments

Hi Aurelio,

Please check if your project interpreter has the path mapping configured, which maps your local project root to `/opt/project` in a docker container. Here's the screenshot for reference:

 

If the mapping doesn't exist, create it manually. This should result in '/opt/project' directory being included in the sys.path automatically.

1

Does anybody know how to do this in IntelliJ with the python plugin?

IT's not there and cannot run things in docker since the path is being forced to /opt/projects instead of respecting the volume tag in the docker-compose file.

4
Avatar
Permanently deleted user

Any instruction on how to check / set this path mappings thing on IntelliJ with Pycharm plugin? I'm having the same problem.

1

To modify the PYTHONPATH in IDEA, the easiest approach I think is to add the PYTHONPATH env variable to your run/debug configuration. The path you specify there will be appended to the PYTHONPATH (not overwritten)

1
Avatar
Permanently deleted user

Thank you, Andrey ! In my first clone of the project, it works without configuring anything, weirdly enough.

0

I've created https://youtrack.jetbrains.com/issue/PY-53434 to try and get some traction in this area.

0

I have the same problem. It is unpredictable what happens. Frustrating.

1

Please sign in to leave a comment.