121đ
PyCharm (or your ide of choice) acts as the âserverâ and your application is the âclientâ; so you start the server first â tell the IDE to âdebugâ â then run the client â which is some code with the settrace
statement in it. When your python code hits the settrace
it connects to the server â pycharm â and starts feeding it the debug data.
To make this happen:
1. copy the pydev
library to the remote machine
So I had to copy the file from C:\Program Files\JetBrains\PyCharm 1.5.3\pycharm-debug.egg
to my linux machine. I put it at /home/john/api-dependancies/pycharm-debug.egg
2. Put the egg in the PYTHONPATH
Hopefully you appreciate that youâre not going to be able to use the egg unless python can find it. I guess most people use easy_install but in my instance I added it explicitly by putting this:
import sys
sys.path.append('/home/john/app-dependancies/pycharm-debug.egg')
This is only necessary because Iâve still had no success installing an egg. This is my workaround.
3. setup the debug server config
In PyCharm you can configure the debug server via:
- Run-> Edit Configurations: opens the âRun/Debug Configurationsâ dialog
- Defaults -> âPython Remote Debugâ: is the template to use
- fill out the local host name and port and youâll probably want to âuse path mappingâ but more on all this belowâŠ
-
âOKâ
Local host name: means the name of the server â thatâs the windows host machine in my case â or actually the IP Address of the windows host machine since the hostname is not known to my remote machine. So the virtual (remote) machine has to be able to reach the host.
ping
andnetstat
are good for this.Port: can be any vacant non-priviledged port you like. eg:
21000
is unlikely to be in use.Donât worry about the path mappings for now.
4. Start the debug server
- Run-> Debug : start the debug server â choose the configuration you just created.
The debug console tab will appear and you should get
Starting debug server at port 21000
in the console which means that the ide debug server is waiting for your code to open a connection to it.
5. Insert the code
This works inside a unit test:
from django.test import TestCase
class APITestCase(TestCase):
def test_remote_debug(self):
import sys
sys.path.append('/home/john/dependancies/pycharm-debug.egg')
from pydev import pydevd
pydevd.settrace('192.168.33.1', port=21000, suspend=False)
print "foo"
And in a django web application itâs a bit finicky about where you put it â seems to work only after everything else is done:
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
sys.path.append('/vagrant/pycharm-debug.egg')
import pydevd
pydevd.settrace('192.168.33.1', port=21000, suspend=False)
Again that the IP address is the box where youâre running Pycharm on; you should be able to ping that ip address from the box running your code/website. The port is your choice, just make sure youâve told pycharm to listen on the same port. And I found the suspend=False
less problematic than the defaults of, not only immediately halting so youâre not sure if itâs working, but also trying to stream to stdin/out which might give you grief also.
6. Open the firewall
Windows 7 firewall will, by default, block your incoming connection. Using netstat on the remote host youâll be able to see that SYN_SENT never becomes ESTABLISHED, at least not until you add an exception to the windows firewall for the application âpycharmâ.
OS/X and Ubuntu do not have firewalls to punch threw (by default, someone may have applied one later).
7. Set a breakpoint and run the code
After all that, when everything goes to plan, you can set a breakpoint â somewhere after the settrace has run â and pycharm console will show
Connected to pydev debugger (build 107.386)
and under the âDebuggerâ tab the variables stack will start working and you can step through the code.
8. Mappings
Mapping tell pycharm where it can find the source code. So when the debugger says âiâm running line 393 of file /foo/bar/nang.py, Pycharm can translate that remote absolute path into an absolute local path⊠and show you the source code.
/Users/john/code/app/ /opt/bestprice/app/
/Users/john/code/master/lib /opt/bestprice/lib/python2.7/site-packages
Done.
12đ
It is just a note , actually , but contains some info that may save hours.
-
Right now
pip install pydevd
worked for me on both ubuntu and centos 6 -
If you want to really debug remote server which is behind firewals and stuff, you can use the following trick:
ssh -R 8081:localhost:8081 user@remote-server.com
This allows remote code to connect to your machine listening on
localhost:8081
-
If remote debugger does not want to start, saying it canât find socket port, check your firewall rules. Note that rule with
127.0.0.1
is not the same aslocalhost
.
- [Django]-Django's Double Underscore
- [Django]-Running Django with FastCGI or with mod_python
- [Django]-Django REST Framework â Separate permissions per methods
1đ
It seems that for some reason debugger couldnât connect to your windows host with PyCharm. Havenât you got any other messages in stderr? If you have not, try to run it one more time, but with sterrToServer=false. That may show real reason why it doesnât connect.
- [Django]-Django filter the model on ManyToMany count?
- [Django]-How to simplify migrations in Django 1.7?
- [Django]-Django model fields validation