如何从命令行调试使用python -m运行的Python模块? [英] How to debug a Python module run with python -m from the command line?
问题描述
我知道可以使用命令行从命令行调试Python脚本
I know that a Python script can be debugged from the command line with
python -m pdb my_script.py
如果 my_script.py
是旨在使用 python my_script.py
运行。
但是,python模块 my_module.py
应该与 python -m my_module
运行。即使包含相对导入的脚本也应使用 python -m
运行。如何在 pdb
的控制下运行 python -m my_module
?以下不起作用:
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
推荐答案
您现在不能这样做,因为 -m
终止选项列表
You can't do it now, because -m
terminates option list
python -h
...
-m mod : run library module as a script (terminates option list)
...
表示要解释其余参数列表是 mod的工作,而此行为完全取决于内部设计 mod 的方式以及它是否支持另一个 -m
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
让我们检查一下 python 2.x 的rel = noreferrer> pdb 。实际上,没有什么有趣的地方,它只希望提供脚本名称:
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
5天前,已合并允许执行您要问的请求请求。多么神秘的巧合!这是代码
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
因此,请稍等一下即将发布的python 3.x版本,以解决此问题)
So just wait a bit for the upcoming python 3.x versions to have this issue resolved )
这篇关于如何从命令行调试使用python -m运行的Python模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!