简体   繁体   中英

Unable to connect to containerized Django web server while running Pycharm remote debugging session

I have a Django application running in a Docker-compose configuration and I would like to be able interactively debug it from Pycharm. I first tried and failed to set up debugging using Pycharm's built-in Docker Compose configuration (result: the application runs and is accessible, but debugging is not available, I suspect because of a known bug where the debugger fails to connect if the entrypoint script takes too long to return ). I am now attempting to set up debugging using remote debugging with pydevd, roughly following the instructions contained here and summarized below:

  • Add a Python Remote Debugger run configuration in Pycharm
  • In my Django application, copy pycharm-debug-py3k.egg to the root directory, add it to the path, import pydevd & initialize it in my app code like so:

    import sys sys.path.append("pycharm-debug-py3k.egg") import pydevd pydevd.settrace('ip.address.of.my.machine', port=4444)

  • Start the debugger in Pycharm

  • Start my application with docker-compose up
  • Press the Play/Resume program button in the debugger

The result of this is that in Pycharm, the debugger console reports that a connection is active ("Connected to pydev debugger"). The debugger tab shows the expected variables and values present in the file I added the code snippet to. No errors appear in the container logs for my Django application and the log shows expected output for normal Django start-up. However, my application is not accessible from a web browser, and attempting to run unit tests in the container hangs with no response. After keyboard interrupting the unit tests I receive a stack trace with this message at the bottom:

   File "/usr/local/lib/python3.6/site-packages/pydevd.py", line 1392, in _locked_settrace
     time.sleep(0.1)  # busy wait until we receive run command
 KeyboardInterrupt

I have deleted all breakpoints, deleted .idea , restarted Pycharm, restarted Docker, and confirmed that if I remove the pydevd code snippet all unit tests and the web server function as expected.

I worked out the answer as soon as I posted- I added the code snippet to the wrong file. When I added it to the __init__.py of my root app and restarted the debugging server, it works as expected, breaking where I've marked breakpoints in Pycharm (when accessed with the web browser at least, still no joy running unit tests).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM