No such file for Docker configuration

I am trying to get PyCharm working with my docker container and having issues. When I try to run a file I get an error that says `IOError: [Errno 2] No such file or directory: '/path/dir/main.py'`. I've been through the tutorials but clearly I'm doing something wrong. I created my Docker container like so:

```docker run -d -i -t --name mydocker -h mydocker \
-p $port:22 \
-e PYTHONPATH=./src:./my/python/path \
-v /my/path:/my/path \
docker_image:10 sudo /sshd.sh```

Then I made it my Python interpreter in PyCharm. But when I open a terminal and ssh into the container, I can see the file in the correct location, so I know it's there. Why can't I access it in PyCharm? Here is my configuration:

My script path is /path/dir/main.py

My Python interpreter is Remote Python 2.7.12 Docker (docker_image:10)

My working directory is /path/dir

I have checked "Add content roots to PYTHONPATH" and "Add source roots to PYTHONPATH". Under Execution, I have check "Run with Python console"

My Docker container settings is -v /path:/opt/project

In my Project Structure, /path is shown as a source folder (on the right) and I've marked everything as "Sources".

I am using the professional version of PyCharm.

4 comments

First of all, ensure the path mappings are configured in the project settings

Also, please try going through the tutorial over here: https://www.jetbrains.com/help/pycharm/using-docker-as-a-remote-interpreter.html

Then, try to setup your project in a similar way. If you still get the issue, let me know.

0

My guess is that my path mappings are not correct. I went through the tutorial and that worked for me, but they don't mention path mappings. I found a site that talks a little about path mappings (https://www.jetbrains.com/help/pycharm/edit-project-path-mappings-dialog.html) but I can't find a page that gives me a broad overview of what they are and how and when to use them. So I don't know what my path mappings should look like. I enter one that was like /path/my_module→/my_module, but it didn't have any effect.

I should note that I get the `IOError: [Errno 2] No such file or directory: '/path/dir/main.py'` only when I have "Run with Python console" selected. If I don't select it, or I enter debug mode, I instead get an ImportError: No module named my_module. I don't understand why it's not being found because it's at /path/my_module and I've marked /path as a source folder.

Based on what I've read (and seen on the console), PyCharm is creating its own container each time I run it, so creating one with a PYTHONPATH and mounting volumes doesn't actually do anything, because that container is not being used. Is this correct?

 

By the way, my overall goal is just to get PyCharm and my import all working in a docker container. So I've also tried doing it with docker-compose. I got stuck with that method as well, but I'm willing to try whichever method is better. If you think the docker-compose method is better, I'll post all my steps to get to where I am with that method.

 

Edit: I saw another place to enter Docker settings, so that might be the issue. I'll update this once I look at that.

0

>Based on what I've read (and seen on the console), PyCharm is creating its own container each time I run it, so creating one with a PYTHONPATH and mounting >volumes doesn't actually do anything, because that container is not being used. Is this correct?

Not sure I understand the question. PyCharm uses it's own helper container for debugging and some connectivity assistance.

If you get ImportError, the environment inside the container don't have the required modules. Most python containers you base your images off don't have any modules except the python built-ins. You need to install them using Dockerfile script, and then build your own image. Have you tried this?

You see Dockerfile example in the docker-compose tutorial. That can also be used for plain docker.

0

Sorry for the delay - they were installed in the Dockerfile script. I've switched to VSCode so thanks for trying to help but you can disregard this.

0

Please sign in to leave a comment.