15
votes

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 ?

1

1 Answers

14
votes

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')"