17

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 ?

Overdrivr
  • 6,296
  • 5
  • 44
  • 70

2 Answers2

15

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')"
Matthias Fripp
  • 17,670
  • 5
  • 28
  • 45
  • I'm on Windows but your answer applies as well, I tried with a relative path and it works. Your answer got me thinking that python is capable of finding the location of a module using the `__path__` attribute so it could be possible for pdb to locate a module file directly. Thanks for your answer – Overdrivr Aug 29 '17 at 07:10
  • Glad it helped. I've also added some Windows options, not sure if these match what you did. – Matthias Fripp Aug 29 '17 at 19:56
  • Thanks, that one liner was exactly what I was looking for – Overdrivr Aug 30 '17 at 04:20
  • Just realized that in your case you could also shorten it to `python -c "import pdb; pdb.run('import mylocalpkg.scriptfile')"` – Matthias Fripp Aug 30 '17 at 04:58
0

One of the things that causes problems here is not where the source files are installed, but syntax used for the import statements.

Python 2 only:

import submodule2

Python 2 and 3:

from . import submodule2

If one sticks to the latter convention, it "just works".

Lastly, highly recommend pudb for on-target debugging. 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)

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 -
Bruce Edge
  • 1,975
  • 1
  • 23
  • 31