Remote Debug Python Django app?

Answered

The remote debugging page (https://www.jetbrains.com/help/idea/remote-debugging.html) implies I must write code to get remote debugging to work. I would like to remote-debug a Django app, so I do not think that is an option.

I would like to execute the `pydev.py` file remotely, much like how it is done locally:

C:\Python37\python.exe C:\Users\kyle\.IntelliJIdea2019.2\config\plugins\python\helpers\pydev\pydevd.py --multiproc --qt-support=auto --client 127.0.0.1 --port 52454 --file manage.py runserver 0.0.0.0:8000


How do I install pydevd-pycharm.egg?  What is the command line to execute it on the remote machine (actually a docker image)?

 

Thank you

 

 

0
3 comments

I can get the two to "connect" (for a moment), but then I reach code that is wrong.   This happens locally (as seen below) and on the docker image.

pip install pydevd
C:\Users\kyle\code\treeherder>c:\Python37\Scripts\pydevd --multiproc --qt-support=auto --client 127.0.0.1 --port 7001 --file manage.py runserver 0.0.0.0:8000
Traceback (most recent call last):
File "c:\python37\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\python37\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "c:\Python37\Scripts\pydevd.exe\__main__.py", line 9, in <module>
File "c:\python37\lib\site-packages\pydevd.py", line 2965, in main
dispatcher.connect(host, port)
File "c:\python37\lib\site-packages\pydevd.py", line 2697, in connect
self.reader = DispatchReader(self)
File "c:\python37\lib\site-packages\pydevd.py", line 2717, in __init__
process_net_command=process_net_command,
TypeError: __init__() missing 1 required positional argument: 'py_db'
0

 

After inspecting some of the codepaths in the PyDev.Debugger project, this seems to work better:

python /usr/local/lib/python3.7/site-packages/pydevd.py --multiprocess --qt-support=auto --client host.docker.internal --port 7001 --file manage.py runserver 0.0.0.0:8000

but there are import problems (which may be some interference between the remote debugger and the tricks inside django)

0

Hi, I am sorry for the late response.

> I must write code to get remote debugging to work

Do you mean inserting pydevd.settrace call in the source code? What exactly is blocking this approach?

>I would like to execute the `pydev.py` file remotely, much like how it is done locally:

Why not using a remote interpreter over SSH? PyCharm will basically call the same local command but on the remote machine once the remote interpreter is set as a project one. Sounds like exactly what you are looking for.

To clarify how the debugger is working under the hood:

  • It contains of two parts: Java/Kotlin frontend (PyCharm UI) and Python backend (patched pydevd)
  • When you start a Python process from PyCharm under the debugger IDE launches both debugger frontend and backend
  • It is not always possible to start a Python process from PyCharm (e.g. Python code is a part of a bigger non-Python application)
  • To be able to debug such cases "Python Remote Debug" configuration exists
  • This configuration starts only the debugger frontend in an infinite loop waiting for a connection from the backend
  • Python backend will be started once pydevd_pycharm call is triggered, this way users can insert this call into their Python source code, start the application which will execute the Python code in turn and wait for pydevd_pycharm to establish a connection to the debugger frontend pausing the execution

As you may see "Python Remote Debug" has a specific role and should not be generally used for other cases. Native support for debugging code over SSH is much more convenient for remote debugging.

We'll change the "Python Remote Debug" run configuration name to remove the obvious confusion in 2020 release, see PY-39230.

In the meantime, the doc page you are referring to is quite outdated, I created a ticket for our technical writer to update it PY-39906. In the meantime please check an up-to-date version here https://www.jetbrains.com/help/pycharm/remote-debugging-with-product.html#remote-debug-config Python plugin for IntelliJ IDEA and PyCharm shares pretty much the same code (except minor UI difference).

PS Don't forget to remove pydevd. For "Python Remote Debug" pydevd-pycharm should be used instead and remote debugging over SSH doesn't require it in the first place. Having pydevd installed will only introduce compatibility issues as PyCharm is using a special patched version.

Sorry for a wall of text, hope it makes sense.

Please feel free to ask any relevant questions here or drop me a message directly at pavel.karateev@jetbrains.com

0

Please sign in to leave a comment.