Search code examples
pythonpython-poetry

How to set up a Poetry project to install module as a runnable Python binary?


I have a Python project that I intend to run as a command line utility and I'm managing its build using Poetry. Through poetry install, I'm able to install a snapshot of the module and import it within the virtualised environment or run it through python -m. However, I want to be able to add it to the shell's path just like how poetry itself end up being runnable without necessarily having to invoke it with python -m. How do I configure my Poetry project to achieve this?


Solution

  • Poetry provides a relatively easy way to add executables via the pyproject.toml, using:

    The example from the docs is:

    [tool.poetry.scripts]
    my_package_cli = 'my_package.console:run
    

    This will add the my_package_cli which executes the run function that would be defined in the my_package.console module.

    So if you use poetry install again after adding the above it should work in the poetry environment, or if you pip install your project anywhere else, it will also work.