You can declare “project scripts” in a pyproject.toml file. When the project is installed using Pip, it generates executable files on the system path (assuming the Python installation’s scripts/ folder is on PATH) which can be called as if they were system executables.

See also

The critical part is declaring the project.scripts table in the pyproject.toml:

...
 
[project.scripts]
print-hello = "foo:main_prints_hello"
 
...

Assuming you have a foo module which exports main_prints_hello(), when the user runs $ print-hello in their system terminal, it is equivalent to:

import sys; from foo import main_prints_hello; sys.exit(main_prints_hello())

This also means that the entry function can:

  • Do typical argparse CLI argument processing
  • Return an int as an error code to sys.exit()

Though I haven’t tested it, I assume that __name__ will NOT be "__main__" in this context.

python