Debugging
The most simple tool is pdb, similar to gdb. However to have a better overview you can use the fullscreen debugger pudb, which requires some more attention.
pdb
This is the one most like gdb, so the first choice probably. Also pudb described in the next section tends to hang completely sometimes so give this a try.
However if you use pytest you have to start it a little different. Actually this way you just continue when nothing goes wrong and you get dropped into pdb on fatal errors. So it might be an idea to always run pytest like this :
| --pdb | |
|---|---|
pudb
First some tips:
- When you startup for the first time you can setup some configuration options which it will then use. These options are kept in ~/.config/pudb/pudb.conf. Alter the options there.
- If you remove the config file you will be asked again at startup. Also you can re-edit the options with : ctrl-p
- Hit ? to see the key bindings.
- Choose an color theme that shows well on dark and that shows the current line in readable colors. 'dark vim' seems to be ok.
unittests
Unit test and pudb bite eachother. If you run a unittest program normally you get :
| unittest | |
|---|---|
If you however run it in the debugger :
| pudb | |
|---|---|
Unittests and pudb do not mix very well, because unittest tries to test the main module which is pudb's main and not your module's main.
To debug a test** function with pudb you should use set_trace() from pudb :
- Add this import at the top of the file : 'from pudb import set_trace'
- Put the set_trace() call at the point where you want the debugger to stop.
- Run the program as normal : './my_program.py'
It will start pudb and halt right after the set_trace() line. Of course this works in any program but for unittest methods it is needed.
complete example:
Start this code as normal! (./test.py) NOT in pudb !(pudb test.py)
eclipse
With pydev this should be possible, i have not tried it yet.
stopping at exception
You can change the exception handler with sys.excepthook, also to startup the debugger. This way you have gdb functionality. Note that this will not work for Flask because Flask already captured that excepthook for you. See chapter below for a Flask solution.
Otherwise place this file in the main directory :
And import that in you main script :
Run this and it will exactly mimic gdb behavior.
Flask pdb
Pylint problem
Pylint does nothing with the sys.path setting unless you put it in ~/.pylintrc
Usually there is an example there, in case of the portal do this :
| pylint problem | |
|---|---|
Since there are flask problems waiting to happen, also do these :
| init-hook | |
|---|---|
Skip that, you will get even more problems so start a python shell :
Copy+paste everything into init-hook='sys.path=[]