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 (, 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/ and tries to run it in D:/.../local/folder/ 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!

Comment actions Permalink

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.

Comment actions Permalink

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.


Please sign in to leave a comment.