ModuleNotFoundError when importing from parent module in container
I am using Docker-Compose as a remote interpreter and using a gunicorn run configuration to launch. This is a work project that is normally launched with a script and works. My folder structure looks like
server/
app/
__init__.py
main.py
__init__.py
My app runs on the container -- files are deployed and code changes are reflected. But the following line in main.py crashes with `ModuleNotFoundError: No module named 'server'`:
from server.app import foo
The app runs and works when I change the import format to "from app import foo".
Some things I have noticed:
- server and app folders are not on the PYTHONPATH or in sys.modules... but this is true even when the program is launched normally with our run script and it is working.
- In the Project window, my remote interpreter has a Remote Libraries section with the whole file system of my container in it. Is this normal?
- When I search for "main.py", I see several results:
- main app
- main .../Pycharm2020.3/docker/<container_name>/app
- main server.app (shown only when I check "Include non-project files"). This points to the version in the Remote Library.
Please sign in to leave a comment.
Another thing I noticed is that when I go up to the definition of an interface function using the up arrow next to it, and then click the down arrow next to it to go back to implementations, I get two entries: The function definition in my code, and the one in the Remote Libraries. I would really like some background on these remote libraries and whether this is normal.
Hi,
It's hard to tell without looking at your environment and the start-up script (which can manipulate the environment).
>In the Project window, my remote interpreter has a Remote Libraries section with the whole file system of my container in it. Is this normal?
This is normal - PyCharm caches remote sources for code insight to work when you edit the file while container is offline.
Could you provide the sys.path printed from PyCharm as well as from running the code via startup script?
Thanks for replying so I don't feel alone. PyCharm Professional is the only way I can find to get remote debugging in Docker, so I want to make it work.
sys.path when I run through the script:
Here's sys.path when I run through PyCharm (after changing "from server.app" to "from app"):
I think the /server/app gets there because it's my WORKDIR in Docker.
My project interpreter currently maps <Project Dir>/Server to /Server because our build process also puts the files in there. But it doesn't work any different if I map <Project Dir> -> /opt/project. Still requires "from app", not "from server.app".
So where is the "app" module mapped to when you run from pycharm? Is it "/opt/project/app"? Should the module be imported from "/opt/project/back-end/server" ? If so, try marking "/opt/project/back-end/" as source root.
I don't see "/opt/project" in the Project window anywhere, but when I marked "back-end" as source root, my project built!
I had had 'server' marked as source root instead, not its parent. Thanks for working through this with such limited information.