Python pdb on python script run as package

孤者浪人 提交于 2019-12-23 07:55:21

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/36227688/python-pdb-on-python-script-run-as-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!