如何调试内置的Python命令,程序包或模块? [英] How do I debug a built-in Python command, package or module?

查看:234
本文介绍了如何调试内置的Python命令,程序包或模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调试Python安装和/或内置软件包附带的一些基本软件包,包括 pip venv

I would like to debug some of the basic packages that come with the Python install and/or are built-in packages, including pip and venv.

希望来自文件权限错误消息(无法访问带有无法打印的文件名的文件)我的团队正在运行这些命令-有关详细信息,请参见此问题

The desire comes from an error message of file permissions (unable to access a file with an "unprintable file name") some of my team is getting running these commands - see this question for details.

在尝试捕获主要python可执行文件中的问题或直接运行基本python模块时如何调试Python源代码(请参见以下示例)对于 pip venv )?

How do you debug the Python source code when trying to catch issues in the main python executable, or when directly running a base python module (see following examples for pip and venv)?

$ python -m pip install --upgrade
$ python -m venv .venv

如果重要的话,我的环境是VSCode,使用bu我可以很高兴地在我编写的任何自定义脚本上使用调试器。与主要的Microsoft Python扩展进行交互的ilt-in调试器。

If it matters, my environment is VSCode, where I am happily able to engage the debugger on any custom script I have written, using the built-in debugger that interacts (I assume) with the main Microsoft Python extension.

推荐答案

从查看以下代码的源代码开始这些模块; -m 开关查找要首先导入的包或模块。如果是软件包,则Python会在该软件包中导入 __ main __ 模块并将其作为主脚本运行。如果是模块,则模块本身将被导入并以 __ main __ 的身份运行。

Start by looking at the source code for those modules; the -m switch looks for a package or module to import first. If it's a package, then Python imports the __main__ module in that package and runs it as the main script. If it is a module, the module itself is imported and run as __main__.

通常 em>代码的结构使得调用一个函数也可以直接导入。然后,您只需编写一些代码即可导入相同的函数,并以与 __ main __ 模块相同的方式调用它。从那里开始,在调试器下运行它很简单。

Usually the code is structured such that a function is called you can import directly too. You can then just write a bit of code that imports the same function and calls it the same way the __main__ module would. From there on out it is trivial to run this under a debugger.

例如 pip 是一个软件包,因此 python -m pip 将导入 pip .__ main __ 并将其作为脚本运行。然后触发:

E.g. pip is a package, so python -m pip will import pip.__main__ and run that as a script. This then triggers:

from pip._internal.cli.main import main as _main  # isort:skip # noqa

if __name__ == '__main__':
    sys.exit(_main())

要运行。您可以在VSCode中执行相同的操作;导入 pip._internal.cli.main.main 并调用它。

to be run. You can do the same in VSCode; import pip._internal.cli.main.main and call it.

您可以查找这些模块的源代码,只需导入它们并打印出结果对象即可:

You can find the source code for these modules by just importing them and printing out the resulting object:

python -c "import pip; print(pip)"

模块的表示形式,如果从磁盘,将包含它的文件名。如果文件名以 / __ init __。py 结尾,那么它是一个程序包,因此您还可以再次检查 __ main __。py 文件存在:

The representation of a module, if loaded from disk, will include it's filename. If the filename ends in /__init__.py it's a package, so you can also double-check that the __main__.py file exists:

python -c "import pip.__main_; print(pip.__main__)"

您可以对 venv 模块。这是Python标准库的一部分,因此文档实际上直接链接到源代码,以及 venv .__ main __ 模块只需导入 venv.main()并调用它即可。

You can do the same for the venv module. This one is part of the Python standard library, so the documentation actually links directly to the source code, and the venv.__main__ module just imports venv.main() and calls it.

这篇关于如何调试内置的Python命令,程序包或模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆