Search code examples
pythonpython-3.xdockershellpython-poetry

Executing custom commands in Python project using Poetry


In my project I have to run the docker build and run commands very frequently to build and run the container respectively. I'm using Poetry as the package manager. I'm looking for a counterpart of npm run <custom-command> to execute my command in project in Python using Poetry. Please help.


Solution

  • You could use the scripts section of the pyproject.toml file to define the custom command: https://python-poetry.org/docs/pyproject/#scripts

    It would look something like:

    [tool.poetry.scripts]
    mycommand = 'mypackage.mymodule:mycommand'
    

    ...where 'mypackage.mymodule:mycommand' is the reference to the location of the function in the project python code where the command is implemented.

    The command itself would be then be run using poetry run <mycommand>