9

I have a python program that I usually run as a part of a package:

python -m mymod.client

in order to deal with relative imports inside "mymod/client.py." How do I run this with pdb - the python debugger. The following does not work:

python -m pdb mymod.client

It yields the error:

Error: mymod.client does not exist

EDIT #1 (to address possible duplicity of question)

My question isn't really about running two modules simultaneously python, rather it is about how to use pdb on a python script that has relative imports inside it and which one usually deals with by running the script with "python -m."

Restated, my question could then be, how do I use pdb on such a script while not having to change the script itself just to have it run with pdb (ie: preserving the relative imports inside the script as much as possible). Shouldn't this be possible, or am I forced to refactor in some way if I want to use pdb? If so what would be the minimal changes to the structure of the script that I'd have to introduce to allow me to leverage pdb.

In summary, I don't care how I run the script, just so long as I can get it working with pdb without changing it's internal structure (relative imports, etc) too much.

David Simic
  • 2,061
  • 2
  • 19
  • 33
  • Possible duplicate of [Multiple -m command line arguments (Python)](http://stackoverflow.com/questions/13423756/multiple-m-command-line-arguments-python) – Nathaniel Ford Mar 25 '16 at 20:53
  • @NathanielFord - see "Edit #1." – David Simic Mar 25 '16 at 21:12
  • I have the exact same issue. Trying to convert my script to a package and not sure how to get -m pdb to work with it – Skotch Oct 20 '16 at 22:02
  • Possible duplicate of [How to debug a Python module from the command line?](https://stackoverflow.com/questions/46265835/how-to-debug-a-python-module-from-the-command-line) – ivan_pozdeev May 30 '18 at 08:37

2 Answers2

2

I think I have a solution.

Run it like this:

python -m pdb path/mymod/client.py arg1 arg2

that will run it as a script, but will not treat it as a package. At the top of client.py, the first line should be:

import mymod

That will get the package itself loaded. I am still playing with this, but it seems to work so far.

Skotch
  • 3,072
  • 2
  • 23
  • 43
  • you saved my day Skotch, especially with the importing of its own package. Works perfectly, thank you! – Michel Müller Oct 25 '17 at 06:43
  • One more thing though: In my case I had to append the working-dir where I'm launching the script to PYTHONPATH using `export PYTHONPATH="${PYTHONPATH}:$(pwd)"`. Otherwise `import mymod` failed with a not-found error. I think python changes the working directory that gets inserted in sys.path to the directory that contains the script, rather than the directory where it is launched. – Michel Müller Oct 25 '17 at 06:58
0

This is not possible. Though unstated in documentation, Python will not parse two modules via the -m command line option.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102