简体   繁体   English

如何使用 entry_point 脚本启动调试器

[英]How to start debugger with an entry_point script

I have a package installed in development mode with pip install -e ./mylocalpkg .我在开发模式下安装了一个包,使用pip install -e ./mylocalpkg

This package defines an entry_points.console_script这个包定义了一个entry_points.console_script

setup(
    name='mylocalpkg',
    ...
    entry_points={
        'console_scripts': [
            'myscript = mylocalpkg.scriptfile:main'
        ]
    },
    ...
)

This script can be called with either way可以用任何一种方式调用这个脚本

$ python -m mylocalpkg.scriptfile
$ myscript

However, I cannot debug this script:但是,我无法调试此脚本:

$ python -m pdb mylocalpkg.scriptfile
Error: mylocalpkg.scriptfile does not exist
$ python -m pdb myscript
Error: myscript does not exist

How can I start a debugging session with pdb while calling entry_point scripts ?如何在调用 entry_point 脚本时使用pdb启动调试会话?

The pdb module must be called with the name of a Python script, not a module.必须使用 Python 脚本的名称而不是模块的名称来调用pdb模块。 So you somehow need to give it a script to run.所以你需要给它一个脚本来运行。

If you're on Linux/Unix/Mac, you're in luck, because myscript is actually a Python script, so you can use one of these options:如果您使用的是 Linux/Unix/Mac,那么您很幸运,因为myscript实际上是一个 Python 脚本,因此您可以使用以下选项之一:

python -m pdb `which myscript`
# or
python -m pdb $(which myscript)

These find the location of myscript and pass it to the pdb module.这些找到myscript的位置并将其传递给pdb模块。 You could also specify the location of myscript directly, if you happen to know that.如果您碰巧知道,您也可以直接指定myscript的位置。

If you're on Windows, you'll need to create a script that loads your entry_point, and then debug that.如果您使用的是 Windows,则需要创建一个脚本来加载您的 entry_point,然后对其进行调试。 Here's a short script that could do the job:这是一个可以完成这项工作的简短脚本:

# run_myscript.py
import pkg_resources
myscript = pkg_resources.load_entry_point('mylocalpkg', 'console_scripts', 'myscript')
myscript()

Then you can debug via this command:然后你可以通过这个命令进行调试:

python -m pdb run_myscript.py

Or, on any platform, you can use this ugly one-liner:或者,在任何平台上,您都可以使用这个丑陋的单行:

python -c "import pdb, pkg_resources; pdb.run('pkg_resources.load_entry_point(\'mylocalpkg\', \'console_scripts\', \'myscript\')()')"

Also, in this particular case, where you want to debug a module that can be loaded via python -m mylocalpkg.scriptfile , you can use a simpler one-liner:此外,在这种特殊情况下,您想要调试可以通过python -m mylocalpkg.scriptfile加载的模块,您可以使用更简单的单行:

python -c "import pdb; pdb.run('import mylocalpkg.scriptfile')"

One of the things that causes problems here is not where the source files are installed, but syntax used for the import statements.这里导致问题的原因之一不是源文件的安装位置,而是用于import语句的语法。

Python 2 only:仅限 Python 2:

import submodule2

Python 2 and 3: Python 2 和 3:

from . import submodule2

If one sticks to the latter convention, it "just works".如果坚持后一种约定,它就“有效”。

Lastly, highly recommend pudb for on-target debugging.最后,强烈推荐pudb进行目标调试。 No setup.没有设置。 just install and go.只需安装即可。 (Although, it's easier to apt-install python3-pudb than try get it installed in the appropriate/same venv) (虽然,apt-install python3-pudb 比尝试将其安装在适当/相同的 venv 中更容易)

Sample command line for a locally installed wheel with a command line entry pointing to this file, and shows args for the script:本地安装的轮子的示例命令行,命令行条目指向此文件,并显示脚本的参数:

pudb3 /opt/venv/xxx/lib/python3.8/site-packages/xxx/create_xxx.py  arg1 --database param1 --host hostbparam.com -

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM