Every debugger I tried out there expects a source file to debug. However Python does not always work this way.
I have a module that is a folder with __init__.py
and __main__.py
files inside, among others, and I usually execute that this way:
$ cd /parent/folder
$ python3 -m module_folder --help
If I don't use -m
, relative imports fail. If I just pass the folder to pudb
, pdb
and others, debugger fails:
$ cd /parent/folder
$ python3 -m pdb module_folder
Traceback (most recent call last):
File "/usr/lib64/python3.3/pdb.py", line 1658, in main
pdb._runscript(mainpyfile)
File "/usr/lib64/python3.3/pdb.py", line 1536, in _runscript
with open(filename, "rb") as fp:
IsADirectoryError: [Errno 21] Is a directory: 'module_folder'
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /usr/lib64/python3.3/pdb.py(1536)_runscript()
-> with open(filename, "rb") as fp:
(Pdb)
How can I debug this? Preferrably with pudb
, and without removing the relative imports.
Put this at the top of your
__main__.py
:This way, these 2 commands become equivalent:
cd /parent/folder; python -m module_folder
python /parent/folder/module_folder/__main__.py
Just use the 2nd syntax for debugging:
or
The same works for Python 2.
You can safely remove from the above code the part
__name__ == '__main__' and
because, as you are writing in__main__.py
, it would always beTrue
. However, it's a common practice to put it. See PEP 366 and this other answer.