gpt4 book ai didi

python - 如何从命令行调试使用 python -m 运行的 Python 模块?

转载 作者:IT老高 更新时间:2023-10-28 20:22:17 25 4
gpt4 key购买 nike

我知道可以从命令行调试 Python 脚本

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?以下不起作用:

python -m pdb -m my_module

最佳答案

你现在不能这样做,因为 -m 终止选项列表

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

这意味着 mod 的工作是解释参数列表的其余部分,这种行为完全取决于 mod 的内部设计方式以及它是否支持另一个 -米

让我们看看 python 2.xpdb 内部发生了什么。实际上,没有什么有趣的,它只需要提供一个脚本名称:

   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)

python 3.x

的当前发布版本相同

好消息

5 天前允许执行您所要求的请求的拉取请求是 merged。多么神秘的巧合!这是 code

所以请稍等一下,等待即将发布的 python 3.x 版本解决此问题)

关于python - 如何从命令行调试使用 python -m 运行的 Python 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46265835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com