How to connect PyCharm to a python interpreter located inside a Docker container?

前端 未结 10 2191
再見小時候
再見小時候 2021-01-30 03:40

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

10条回答
  •  花落未央
    2021-01-30 04:01

    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.

提交回复
热议问题