Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 314
Ritik Saxena Avatar asked Jul 26 '26 18:07

Ritik Saxena


2 Answers

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>

like image 122
elukem Avatar answered Jul 28 '26 07:07

elukem


The solution was found when the project was initialized through poetry init, followed by writing code and installing dependencies. As a result, poetry install still throws an error about system arguments.

The solution for my case was:

  • Creating a project via poetry new <your_project_name>
  • Going to the project folder via cd
  • Writing code, specifically the structure that will be called in [tool.poetry.scripts]
  • Install dependencies via poetry add
  • Configuration via poetry install
like image 29
Raymond Avatar answered Jul 28 '26 08:07

Raymond



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!