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.

2
  • "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. Commented Jun 4, 2021 at 4: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! Commented Jun 6, 2021 at 15:56

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.