0

For example, using -m for pdb and post-mortem debugging, you can do:

python -m pdb script.py arg1 --kwarg

(say for instance that arg1 and kwarg are parsed from sys.argv with the argparse module).

A CLI tool can be created with pip/setuptools' entry points:

setup(
    ...
    entry_points={
        "console_scripts": [
            "toolname = my_project.script:main"
        ]
    },
)

Now after installing pip install my_project, toolname can be used instead of python /path/to/script.py. Is it possible to pass arguments directly to python - like -m to invoke pdb?

Edit:

Unlike python setup tools console_scripts with arguments I know how to pass arguments to the script, and I'm looking for how to pass arguments to python itself.

The python -m pdb $(which myscript) method, used in How to start debugger with an entry_point script is pretty good, but it only works if the script actually calls main() when executed (script may be from a third-party library).

python -c "import pdb, pkg_resources; pdb.run('pkg_resources.load_entry_point(\'mylocalpkg\', \'console_scripts\', \'myscript\')()')" supports more scripts and platforms, but I'm not sure if it works for other python options other than -m pdb.

user2561747
  • 1,333
  • 2
  • 16
  • 39
  • "Is it possible to pass arguments directly to python - like `-m` to invoke `pdb`?" The arguments passed would be handled directly by your script, your script will have full control and view of all arguments, including invoking `pdb` at specifc step; alternatively if you want to start the `toolname` script with `-m pdb` the other answer linked has the answer. It was not clear from the question which of the two you want. – metatoaster Jun 04 '21 at 04:42
  • Thanks, I couldn't find the `entry_points` answer (I was searching for console scripts). It's fairly close, and helps my problem, thanks! – user2561747 Jun 06 '21 at 15:56

0 Answers0