10

I can't find how to debug custom management commands.

When errors happen, they just say something like :

IndexError: list index out of range

How to get more debugging info ?

Like the number of the line where it crashes for instance, that would be helpful.

3
  • import traceback; traceback.print_exc() or just remove wrong code from try/except block temporary. Commented Jul 16, 2013 at 16:27
  • Can't tell much without the code. You can use try/expect or an if on yourlist[0] to avoid the error Commented Jul 16, 2013 at 18:00
  • 1
    Use pdb. import pdb; pdb.set_trace() as the first line in management command and step through the code. Commented Jul 16, 2013 at 18:12

3 Answers 3

20

There is a traceback option that does the trick :

python manage.py command_name --traceback

The command then outputs usual python errors

Sign up to request clarification or add additional context in comments.

1 Comment

You should accept this as the correct answer. Very helpful, thanks.
4

use -i option and pdb:

python -i manage.py command_name

failed or not , you will have a python repl, so you can use pdb to jump into the stacks raising exception:

    return executor(sql, params, many, context)
  File "/home/gary/PycharmProjects/borges-env/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute 
    return self.cursor.execute(sql, params)
  File "/home/gary/PycharmProjects/borges-env/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__          
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/gary/PycharmProjects/borges-env/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute 
    return self.cursor.execute(sql, params)                                                                                    
django.db.utils.DataError: value too long for type character varying(10)                                                       

>>> import pdb                                                                                                                 
>>> pdb.pm()                                                                                                                   
> /home/gary/PycharmProjects/borges-env/lib/python3.6/site-packages/django/db/backends/utils.py(84)_execute()                 
-> return self.cursor.execute(sql, params)                                                                                                                   
(Pdb) param  
blablabalablabalablbalbalabla

Comments

1
  1. Use CommandError
  2. Use the python debugger.
  3. Use exceptions.
  4. Use an error logging service.

1 Comment

first link is broken

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.