Debugging Django from the command-line - django

I'm working on debugging Django from the command-line. I'm working with django_extensions, and I have IPython installed and ideally I'd like to be able to extract as much information in the shell as possible. How would I go about this task most efficiently?

As mentioned by Geo manage.py shell is good but since you have django_extensions already installed then Carl's suggestion of manage.py shell_plus is even better... saves a ton of typing.
But, as a third suggestion that is a bit less general, you might also want to check out django-viewtools. I personally tend to use shell_plus, but this might be useful.

If you have django_extensions installed, use
python manage.py shell_plus
to get all of your model classes automatically pre-loaded.

How about:
python manage.py shell
More info here

My favorite ways of debuging django problems is using the ./manage runserver_plus command to the django_extensions. It uses the Werkzeug debuger, which gives you a python shell in the web browser itself. If you want a shell anywhere in the code, just type some bogus python, like a simple a, and you will get a shell when that code is executed.
Another nice tool is ipdb ipython debugger. Its the same as pdb but better (and uses ipython. With this, and runserver_plus, you can insert import ipdb; ipdb.set_trace(), and you will get a ipython shell with an debugger in the same window as you have runserver_plus.
Take a look at http://docs.python.org/library/pdb.html#debugger-commands for a list of commands you can use inside the debugger.

Related

spyder profiler uses wrong python

I am using a virtualenv, but I would like to use an IDE. So I pull up spyder and easily I can change the python interpreter to .virtualenv/myenv/.../python
But, when I go to "F10" for to profile my code, Spyder identifies the interpreter in usr/lib/. I couldn't find anywhere in preferences to change this.
How can I make profiler see the correct python interpreter?
-Chris
Hmm it seems like this is a bug, can you post it in the Spyder issue tracker?
You can post an issue from within Spyder itself in the help menu click on "Report issue

Using pypy instead of psyco

Hi I am trying to make use of pypy on my Python 2.7 application running in windows XP.
Just read a little on psyco and it seems pretty easy to use see http://psyco.sourceforge.net/psycoguide/node8.html
if __name__ == '__main__':
# Import Psyco if available
try:
import psyco
psyco.full()
except ImportError:
pass
# ...your code here...
However I realised that psyco doesn't support python 2.7, shame, so pypy being the new hotness, I was wondering how to make use of it in my application. I can't seem to see how I "import" it and make it work as the example above.
My application is not just a single .py file but an entire text editor I am writing using pyqt, I would very much like to speed things up.
Any suggestions of how to use pypy similar to the above code
Any alternatives that would allow me to do something similar to the above?
Many thanks
I just want to clarify, maybe it wasn't clear earlier. currently i use py2exe to create an executable of my application. I have a setup.py file and I run python setup.py py2exe, this creates the executable that I distribute. Now I want to speed up the application so that when I distribute it, the entire application runs faster.
Now how do I make use of pypy again to create a faster executable application that I can distribute?
Pypy is run instead of the CPython executable.
So, when you up until now ran
python mypythonfile.py
Or similar, with pypy you just run
pypy mypythonfile.py
It is a drop-in replacement. You do not need to import anything, especially not psyco

Is there a way to make aptana 3 autoreload when i make changes to a py file in a django project?

Aptana makes it open in --noreload mode automatically because not doing so causes python instances to remain open or something like that, is there anyway to bypass this problem?
As far as I know, it's not possible to auto-reload .py files within an Aptana Python run/debug configuration (which is what I'm assuming you're doing to get access to the call stack for debugging). I've been using Aptana for Python development for a couple of years, and I've yet to see a way to do this. if someone knows how, please share!

debugger for django

how can i debug my django code ?
Generally, I use django-debug-toolbar for debugging django-specific stuff.
pdb is useful for lower-level shell debugging, when you're using runserver. Though I prefer to use ipython (pip install ipdb, then import ipdb; ipdb.set_trace(), like you would with pdb)
There's also werkzeug, which, when combined with django-extensions' runserver_plus command, will allow you to open a web-based python shell on error pages:
This item requires that you have the Werkzeug WSGI utilities (version 0.3) installed. Included with Werkzeug is a kick ass debugger that renders nice debugging tracebacks and adds an AJAX based debugger (which allows to execute code in the context of the traceback’s frames). Additionally it provides a nice access view to the source code.
What's wrong with the built-in Python debugger? Just insert this in your code where you want to set a breakpoint:
import pdb; pdb.set_trace()
and execution will pause there, and the console will show a debugging prompt enabling you to inspect variables and step through the code.
Get PyCharm from JetBrains. It has a lovely built in debugger and Django support.
I use Aptana Studio and pyDev - and eclipse IDE. its free and has build in django and debugging.
http://www.aptana.com/

is there a way to get django tests + buildout run well integrated with Eclipse/Aptana?

it's fairly easy to set up Eclipse to run a django project's tests created with django-admin startprojects, we whould just point a Run command to ./manage.py, and supply the necessary arguments.
but what can I do if a project is built using buildout?
of course, bin/biuldout creates the handy bin/test-1.1 and bin/test-trunk files, but these are not integrated with eclipse
I can even run these as an external tool from Eclipse, but how can I get the nice code recognition of running manage.py test myapp?
does anyone know a solution? is there some buildout recipe that allows me to do this?
thanks for your help!
I'm assuming you use the djangorecipe recipe (http://pypi.python.org/pypi/djangorecipe)?
In that case, why can you apparently run "manage.py test myapp" and why can't you run "bin/django test myapp"? (Or "bin/test" for that matter)?
I suppose you have to configure that manage.py call somewhere, so why can't you configure bin/django in the same way? In the end, bin/django is just a python script, too. Have you tried calling it in the same way as manage.py?