Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyCharm remote debugging - connects but can't start debugging

The basics:

  • I have a django project that runs fine (both locally and on a remote server).
  • I start pycharm locally using a "remote debugging" profile and it looks fine.

    Starting debug server at port 4000
    Use the following code to connect to the debugger:
    import pydevd
    pydevd.settrace('<my local machines public ip addr>', port=4000, suspend=False)
    Waiting for connection...
    
  • On the remote server, I put the appropriate call to pycharm-debug.egg in __init__.py (but I've tried putting it in other places in the code, too).

  • I start the remote server like this python manage.py runserver <remote domain name>:8000 and on the local pycharm IDE/debugger I see:

    "Connected to pydev debugger (build 133.1347)"
    
  • After this, things just sit there... I don't get any feedback on where execution is and no ability in the pycharm IDE to "start/continue/run" anything.

  • On the remote server if I <ctrl-c> it, I see:

    File "/<path here>/pycharm-debug.egg/pydevd.py", line 1256, in settrace
    File "/<path here>/pycharm-debug.egg/pydevd.py", line 1305, in _locked_settrace
    

My guess is that everything is connected properly but I am not seeing a way to start everything running. I have "suspend=False" so I would have expected it to start. When I try to bring up a page against the remote server, I get "not connected" so the web server is obviously not running yet (if curl http://<remote server domain>:8000 I get curl: (7) Failed connect to <remote server domain>:8000; Connection refused). Without remote debugging, this works fine running on either my local dev machine or on the remote server.

What am I doing wrong? :) Thanks!

Details:

  • On remote server that is running the code/process I want to debug in main __init__.py I have :

    import os
    import sys
    BASE_DIR = os.path.dirname(os.path.dirname(__file__))
    sys.path.append(BASE_DIR + '/pycharm-debug.egg')
    import pydevd
    pydevd.settrace('<my local machines public ip addr>', port=4000, suspend=False)
    
  • Local pycharm IDE shows this:

    Starting debug server at port 4000
    Use the following code to connect to the debugger:
    import pydevd
    pydevd.settrace('localhost', port=4000, suspend=False)  (tried with <public ip addr> for this, too.
    Waiting for connection...
    ---- and then, after I start the remote server process: ----
    Connected to pydev debugger (build 133.1347)
    

Other notes:

  • I've tried this both with and without virtualenv on the remote server.
  • I'm running the local pycharm IDE behind a normal Comcast cable modem router with port-forwarding for port 4000 turned on (and tested this with a node server attaching to port 4000 that worked fine when I did a wget against it from my remote server).

I'm at a loss as to how to "bump" anything to get things running and let me start debugging.

Thanks for trying to help.

like image 842
John Q Avatar asked May 25 '14 02:05

John Q


People also ask

What is remote debugging PyCharm?

Remote Debugging with PyCharm Last modified: 21 July 2022. Professional feature: download PyCharm Professional to try. With PyCharm you can debug your application using an interpreter that is located on the other computer, for example, on a web server or dedicated test machine.


2 Answers

For future googlers ... I was able to get remote debugging working, but not via this method. I just answered a similar question about this over on the pycharm forums so thought I'd update this question with the method that did work for me.

  1. Go to "PyCharm/Preferences/Project Interpreter".
    • You'll see an option to choose which python interpreter at the top of the dialog. Next to that is a gear icon. If you click on the gear icon, you'll see an option to "Add remote" ... you give it ssh credentials (or path to ssh keys) and the path to where your python interpreter is installed on the remote server (i.e., /usr/local/bin/python).
    • In addition, the "pycharm helpers path" for me was (/home/<username>/.pycharm_helpers — I can't remember if this was created automatically or not).
  2. Go to "Run / Edit Configurations..." and add a "Django Server" (plus sign at top left of dialog).

    • Choose your new remote python interpreter as the interpreter to use (it should show up in a drop-down list of choices).
    • In the "env vars" section I needed to specify my main app settings file (i.e., DJANGO_SETTINGS_MODULE = <app>/settings.py). For my purposes I also needed to set HTTPS=1.
    • Set the working directory to wherever your django project is on the remote server (i.e., /home/<username>/<xyz>/<appdir>).
    • Set the path mappings from your local dir to the remote dir (i.e., /Users/JohnQ/<xyz>/<appdir>=/home/<username>/<xyz>/<appdir>).

    • Because I needed other 3rd party servers (like FB, etc.) to be able to hit this server using HTTPS, I used "stunnel" on my remote server – it was pretty easy to set up).

  3. In addition to this, a handy thing to do is set up deployment confirmation as well so that you can just right-click to upload newer versions of your file (under "Tools / Deployment / Configurations...").

    • Create a new one and under connection just use ssh creds or path to your keys.
    • In the mappings tab, the paths are the same as what you did for the path mappings for the remote server setup.
    • "Web path on server" was just "/" for me. After you create it, you should just be able to right-click on any file/dir and choose "Upload to...". Note that for an initial upload I just scp'd a tar.gz up to my server to save time and I only upload via the deployment configuration for the changes I do during debugging.

I have been happily using this for remote debugging for ~4 mos., so it works fine.

like image 154
John Q Avatar answered Sep 20 '22 11:09

John Q


I had the same symptoms and "fixed" it by turning off all Python Exception Breakpoints in the View Breakpoints window.

like image 43
user2016190 Avatar answered Sep 19 '22 11:09

user2016190