Can you include a standalone executable in a Python package with pyproject.toml?

Can you include a standalone executable in a Python package with pyproject.toml?
python
Ethan Jackson

Looking at Python packaging with pyproject.toml, I wonder whether it is possible to add a standalone executable (with #!/usr/bin/env python in the first line) to pyproject.toml, without going for a [project.scripts] entrypoint definition and the corresponding auto-generated wrapper. Do I understand right that pyproject.toml supports only the [project.scripts] entrypoint definitions as a way to define executables?

I don't have anything against the entrypoints. It makes the definition of Python executables platform-independent. But it's just a curious aspect of pyproject.toml.

Specifically, is it possible to have a project like this:

pyproject.toml my_executable.py my_packet/ __init__.py my_module.py

Where my_executable.py has the +x permissions bits for execution and contains code like this:

#!/usr/bin/env python from my_packet.my_module import func def main(): return func() if __name__ == '__main__': main()

For completeness, the same in the entrypoint way:

pyproject.toml my_packet/ __init__.py my_module.py my_executable.py

With pyproject.toml:

... [project.scripts] my-executable = "my_packet.my_executable:main"

Where my_executable.py is:

from my_packet.my_module import func def main(): return func() # may not have if __name__ at all # or contain not application code # like some tests

Then the executable gets generated during installation. And it looks like this:

#!/home/.../my_venv/bin/python # -*- coding: utf-8 -*- import re import sys from my_packet.my_executable import main if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) sys.exit(main())

Answer

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answe que r

Related Articles