Debugging

Using PyDev/Eclipse

Prerequisites

Make sure that Pydev is on the PYTHONPATH in the virtualenv you are using:

echo "/opt/eclipse/plugins/org.python.pydev_2.7.1.2012100913/pysrc/" > $VIRTUAL_ENV/lib/python*/site-packages/pydev.pth

Make sure that Project | Properties | Pydev - Django is set up correctly:

  • Django manage.py = manage.py

  • Django settings module = fdw.settings.local

If there are any other source directories that you want to incorporate from outside the site-packages directory you can also include them:

echo "/home/roger/git/django-database-files/database_files/" > $VIRTUAL_ENV/lib/python*/site-packages/database_files.pth

Debugging from within Eclipse

Create a debug configuration with:

  • Name: runserver

  • Main Module: ${workspace_loc:fdw/manage.py} # change the app name as required

  • Arguments: runserver 8001 --noreload

  • Environment: create the following environment variables:

  • PGHOST=127.0.0.1

  • PGPORT=5432

  • PGDATABASE=pricedev

  • PGUSER=price_owner

  • PGPASSWORD={database password}

Remote Debugging

  • Open the Debug perspective

  • Add import pydevd;pydevd.settrace() to the file you want to start the debugger in

  • Select the Pydev | Start Debug Server menu option

  • Run the appliction, e.g. ./manage.py runserver

Note that if you leave the pydevd.settrace() command in a file and then run it without the Pydev Debug Server running you will get an error:

error: [Errno 111] Connection refused

Profiling

Line Profiler

Decorate functions with @profile (it doesn’t need to be imported first), and run the program with:

kernprof –line-by-line –view ./manage.py runserver –settings=fdw.settings.insecure –noreload

Note that you have to run Django with --noreload to prevent runserver from starting a separate process for the webserver while it watches for code changes, so that the profile function that kernprof inserts into builtins is visible. Note that the profiler writes out all the profile data when kernprof exits, so start the application server using kernprof, submit the test request and then press Ctrl-C to exit the application and write the profile information.