Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start debugger with an entry_point script

Tags:

python

I have a package installed in development mode with pip install -e ./mylocalpkg.

This package defines an 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 ?

like image 923
Overdrivr Avatar asked Aug 29 '17 04:08

Overdrivr


People also ask

How do I Debug a script in blender?

Open up Blender's search (default shortcut: F3), type "Debug". Click Debug: Start Debug Server for VS Code .

How do I run a Python script in Debug mode in Linux?

To start debugging within the program just insert import pdb, pdb. set_trace() commands. Run your script normally, and execution will stop where we have introduced a breakpoint. So basically we are hard coding a breakpoint on a line below where we call set_trace().

How do I run a Debug script in PyCharm?

To quickly set up a run/debug configuration, place the caret at the declaration of the executable class or its main method, press Alt+Enter and select Edit. If you already have a run/debug configuration, and it is currently selected in the run/debug configurations list, press Shift+F9 .


1 Answers

The pdb module must be called with the name of a Python script, not a module. 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:

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

These find the location of myscript and pass it to the pdb module. You could also specify the location of myscript directly, if you happen to know that.

If you're on Windows, you'll need to create a script that loads your entry_point, and then debug that. 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 -c "import pdb; pdb.run('import mylocalpkg.scriptfile')"
like image 187
Matthias Fripp Avatar answered Oct 16 '22 22:10

Matthias Fripp