How to connect PyCharm to a python interpreter loc

2019-03-07 21:59发布

I'm starting with Docker, but I don't know how to configure PyCharm to use a python interpreter located in a container.

It was easy to setup with Vagrant, but there's apparently no official way to do it with Docker yet.

Should I prepare special Docker image with exposed ssh port? How to do that more easily?

10条回答
淡お忘
2楼-- · 2019-03-07 23:00

In order to avoid any SSH overhead (which makes perfect sense with Docker), docker exec definitely seems to be the way to go.
Unfortunately I couldn't get it to work so far. It would be great if someone could fill in the blanks. Here is what I did (using PyCharm 4.0.4 and Docker 1.4.1):

  1. Create a file named python_myproject.sh containing the following:

    #!/bin/bash
    docker exec -i myproject_container /path/to/containers/python2.7
    

    Note that the file's name has to begin with python otherwise PyCharm will complain.

  2. In PyCharm's settings, under Project Interpreter, add a new local interpreter. Give it the path to your python_myproject.sh file.


This is where I'm stuck. After a quite long loading time (the throbber says "Setting up library files"), a window entitled "Invalid Python SDK" appears and says:

Cannot set up a python SDK
at /path/to/python_myproject.sh.
The SDK seems invalid.

In ~/.PyCharm40/system/log/.idea:

2015-02-19 17:33:30,569 [ 166966]   WARN - ution.process.OSProcessHandler - Cannot kill process tree. Trying to destroy process using Java API. Cmdline:
2015-02-19 17:34:30,628 [ 227025]   WARN - ution.process.OSProcessHandler - Cannot kill process tree. Trying to destroy process using Java API. Cmdline:
2015-02-19 17:34:30,653 [ 227050]   INFO - rains.python.sdk.PythonSdkType - 
Timed out
查看更多
贪生不怕死
3楼-- · 2019-03-07 23:01

With Docker 1.3, use the exec command to construct the path to the Python interpreter:

sudo docker exec container_name /usr/bin/python

See https://docs.docker.com/reference/commandline/cli/#exec, http://forum.jetbrains.com/thread/PyCharm-2224

You could install SSH inside the container and then expose the port, but that isn't how containers are expected to be used, because you would be bloating them.

查看更多
我命由我不由天
4楼-- · 2019-03-07 23:02

If all you need is to debug code which is launched inside docker container, you could use pycharm's python debug server feature. As for me, it is less troublesome way than accessing remote interpreter via SSH. Drawback of this solution is that for auto-complete and all this kind of stuff you should have local copy of container's interpreter and mark it as project's interpreter (works for auto-complete, but i'm not sure that it's possible to debug code from third-party libs in such case) or make container's interpreter files visible to pycharm (not tested at all). Also note that Python debug server is feature of Professional edition.

What you should do for debugging via Python debug server:

1) make sure that directory with your project is added into container. It could look like this line in Dockerfile:

ADD . /path/in/container

2) copy pycharm-debug.egg (pycharm-debug-py3k.egg for Python3) from directory where pycharm is installed on your host to directory in container, which is in container's PYTHONPATH. Path to pycharm-debug.egg on developer's host could be:

  • for Mac: /Applications/PyCharm.app/Contents/pycharm-debug.egg
  • for Linux: /opt/pycharm/pycharm-debug.egg

3) create Run/Debug configuration for launching Python debug server on host as described at To configure a remote debug server section of docs. Port is any host's port of your choice, but IP is address at which host is accessible from container. It could be:

  • if container run via boot2docker, likely, IP is 192.168.99.1 -- host's address at Host-only network with vbox machine
  • if host is Linux, IP can be found via ifconfig, for me it is:
docker0   Link encap:Ethernet  HWaddr 56:84:7a:fe:97:99  
          inet addr:172.17.42.1  Bcast:0.0.0.0  Mask:255.255.0.0

Also, don't forget to specify path mappings between project's path at developer's host and project's path at container.

This blog post also could be helpful for current step

4) launch this created configuration (for example, via Debug button, right from Run one)

5) create python script which would launch your project and add the following code for debug initialization as first lines of this script. (make sure that pycharm-debug.egg is in PYTHONPATH, or this code couldn't import pydevd):

   import pydevd
   pydevd.settrace('172.17.42.1', suspend=False, port=8765, stdoutToServer=True, stderrToServer=True)

6) Finally, you could set breakpoints and launch your application from host, in container via created script. For example:

docker-compose run 'container_name' python 'script_name' 'args'

On start, yours launching script will connect to Python debug server, which is running on host, and stop on breakpoints. Debugger features will be available as usual.

查看更多
迷人小祖宗
5楼-- · 2019-03-07 23:03

With PyCharm 5 they added support for docker. You must have your docker configured in docker-machine.

If you don't already use docker-machine you can connect to an existing machine using the generic machine engine and ssh into a vagrant VM or to localhost if you aren't running things in a VM. I didn't find a way around the ssh to localhost unfortunately.

I haven't found a way to mount volumes into the docker image they use, to share files with my dev tree, but it might be possible.

查看更多
登录 后发表回答