Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug a Python module run with python -m from the command line?

I know that a Python script can be debugged from the command line with

python -m pdb my_script.py 

if my_script.py is a script intended to be run with python my_script.py.

However, a python module my_module.py should be run with python -m my_module. Even scripts that contain relative imports should be run with python -m. How can I run python -m my_module under pdb's control? The following does not work:

python -m pdb -m my_module 
like image 686
Alexey Avatar asked Sep 17 '17 15:09

Alexey


People also ask

How do I run a Python script in debug mode from command line?

Starting Python Debugger To start debugging within the program just insert import pdb, pdb. set_trace() commands. Run your script normally and execution will stop where we have introduced a breakpoint.

Can you use GDB with Python?

Pretty much anything that can be done at the GDB command line can be done with a breakpoint from Python. You can attach commands to a breakpoint, make it conditional, ignore a certain number of hits, make the breakpoint specific to a thread or process, and all of the things you can do from the command line.

How do you pass command line arguments in Visual Studio Python?

To set command-line arguments in Visual Studio, right click on the project name, then go to Properties. In the Properties Pane, go to "Debugging", and in this pane is a line for "Command-line arguments." Add the values you would like to use on this line. They will be passed to the program via the argv array.


1 Answers

You can't do it now, because -m terminates option list

python -h ... -m mod : run library module as a script (terminates option list) ... 

That means it's mod's job to interpret the rest of the arguments list and this behavior fully depends on how mod is designed internally and whether it support another -m

Lets check out what's happening inside pdb of python 2.x. Actually, nothing intereseting, it only expects a script name to be supplied:

   if not sys.argv[1:] or sys.argv[1] in ("--help", "-h"):         print "usage: pdb.py scriptfile [arg] ..."         sys.exit(2)      mainpyfile =  sys.argv[1]     # Get script filename     if not os.path.exists(mainpyfile):         print 'Error:', mainpyfile, 'does not exist'         sys.exit(1)      del sys.argv[0]         # Hide "pdb.py" from argument list      # Replace pdb's dir with script's dir in front of module search path.     sys.path[0] = os.path.dirname(mainpyfile)      # Note on saving/restoring sys.argv: it's a good idea when sys.argv was     # modified by the script being debugged. It's a bad idea when it was     # changed by the user from the command line. There is a "restart" command     # which allows explicit specification of command line arguments.     pdb = Pdb()     while True:         try:             pdb._runscript(mainpyfile) 

Same for the currently released versions of python 3.x

Good news

The pull request that allows to do what you're asking has been merged 5 days ago. What a mysterious coincidence! Here's the code

So just wait a bit for the upcoming python 3.x versions to have this issue resolved )

like image 157
ffeast Avatar answered Sep 18 '22 09:09

ffeast