How can I create a command line tool and execute it like a script in Python
What I mean by this, is for example, I can download mypy with:
pip install -U mypy
Then, I can run mypy directly into the terminal with:
mypy <app>
How is this be done without needing to write the full python3 -m mypy.py <app>? E.g. if I wanted to convert a Python project made with poetry into a command line tool, without needing to specify python3 / .py?
This way non-Python users can invoke the tool without knowledge of Python, poetry, or virtual envs?
8 comments · 63% upvoted · other discussions
Assuming that they have their environment set up properly, you add a line to your
pyproject.tomlthat tells Poetry to create another Python script in thebinfolder.Suppose that I have the following structure for a project.
The
myechopackage contains amain.pyfile as follows:My
pyproject.tomlfile contains an entry that will tell it to produce an executable script that will invoke thatmainfunction.The name of the executable is the lvalue, and the string is
<package>.<module>:<function>.Building the project and installing the resulting
.whlwill put an executable script calledmyechoin the exact same location aspip. So, for example:I'm using a virtualenv, but this will work with the system Python environment as well.
Solsticized [OP] 2 points · Nov 2023
Thank you for your comprehensive reply! I had no idea about the
[tool.poetry.scripts]part of poetry. I feel like a lot of these script entrypoints are pretty obscure in Python. Thanks again