[英]How do I debug a built-in Python command, package or module?
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
.我想调试 Python 安装附带的一些基本包和/或内置包,包括
pip
和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.愿望来自文件权限的错误消息(无法访问具有“不可打印文件名”的文件)我的一些团队正在运行这些命令 - 有关详细信息,请参阅此问题。
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 可执行文件中的问题时,或者在直接运行基本 python 模块时(请参见以下
pip
和venv
示例),您如何调试 Python 源代码?
$ python -m pip install --upgrade
$ python -m venv .venv
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.如果重要的话,我的环境是 VSCode,我很高兴能够在我编写的任何自定义脚本上使用调试器,使用与主要 Microsoft Python 扩展交互(我假设)的内置调试器。
You will need to set"justMyCode": false
in your launch.json
for the debugger to trace into third-party code.您需要在
launch.json
中设置"justMyCode": false
以便调试器跟踪第三方代码。
Start by looking at the source code for those modules;首先查看这些模块的源代码; the
-m
switch looks for a package or module to import first. -m
开关首先查找要导入的包或模块。 If it's a package, then Python imports the __main__
module in that package and runs it as the main script.如果它是一个包,那么 Python 会在该包中导入
__main__
模块并将其作为主脚本运行。 If it is a module, the module itself is imported and run as __main__
.如果它是一个模块,模块本身被导入并作为
__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.然后,您只需编写一些代码来导入相同的函数并以与
__main__
模块相同的方式调用它。 From there on out it is trivial to run this under a debugger.从那时起,在调试器下运行它就变得微不足道了。
Eg pip
is a package, so python -m pip
will import pip.__main__
and run that as a script.例如
pip
是一个包,因此python -m pip
将导入pip.__main__
并将其作为脚本运行。 This then triggers:然后触发:
from pip._internal.cli.main import main as _main # isort:skip # noqa
if __name__ == '__main__':
sys.exit(_main())
to be run.被运行。 You can do the same in VSCode;
你可以在 VSCode 中做同样的事情; import
pip._internal.cli.main.main
and call it.导入
pip._internal.cli.main.main
并调用它。
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)"
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:如果文件名以
/__init__.py
结尾,则它是一个包,因此您还可以仔细检查__main__.py
文件是否存在:
python -c "import pip.__main_; print(pip.__main__)"
You can do the same for the venv
module.您可以对
venv
模块执行相同的操作。 This one is part of the Python standard library, so the documentation actually links directly to the source code , and thevenv.__main__
module just imports venv.main()
and calls it.这是 Python 标准库的一部分,因此文档实际上直接链接到源代码,而
venv.__main__
模块只是导入venv.main()
并调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.