how to run a script using pyproject.toml settings and poetry? how to run a script using pyproject.toml settings and poetry? python python

how to run a script using pyproject.toml settings and poetry?


At the moment the [tool.poetry.scripts] sections is equivalent to setuptools console_scripts.

So the argument must be a valid module and method name. Let's imagine within your package my_package, you have log_revision.py, which has a method start(). Then you have to write:

[tool.poetry.scripts]my-script = "my_package.log_revision:start"

Here's a complete example:

You should have this folder structure:

my_package├── my_package│   ├── __init__.py│   └── log_revision.py└── pyproject.toml

The content of pyproject.toml is:

[tool.poetry]name = "my_package"version = "0.1.0"description = ""authors = ["Your Name <you@example.com>"][tool.poetry.dependencies]python = "^3.8"[tool.poetry.scripts]my-script = "my_package.log_revision:start"[build-system]requires = ["poetry_core>=1.0.0"]build-backend = "poetry.core.masonry.api"

and of log_revision.py:

def start():    print("Hello")

After you have run poetry install once you should be able to do this:

$ poetry run my-script  Hello

You cannot pass something to the start() method directly. Instead you can use command line arguments and parse them, e.g. with pythons argparse.


Tinkering with such a problem for a couple of hours and found a solution

I had a task to start the django server via poetry script.

Here are the directories. manage.py is in test folder:

├── pyproject.toml├── README.rst├── runserver.py├── test│   ├── db.sqlite3│   ├── manage.py│   └── test│       ├── asgi.py│       ├── __init__.py│       ├── __pycache__│       │   ├── __init__.cpython-39.pyc│       │   ├── settings.cpython-39.pyc│       │   ├── urls.cpython-39.pyc│       │   └── wsgi.cpython-39.pyc│       ├── settings.py│       ├── urls.py│       └── wsgi.py├── tests│   ├── __init__.py│   └── test_tmp.py└── tmp    └── __init__.py

I had to create a file runserver.py:

 import subprocess                                                                                                                                                    def djtest():                                                                         cmd =['python', 'test/manage.py', 'runserver']                                                                       subprocess.run(cmd)                                                    

then write the script itself pyproject.toml:

[tool.poetry.scripts]                                                           dj = "runserver:djtest"  

and still make changes to pyproject.toml:

[tool.poetry.scripts]                                                           dj = "runserver:djtest"

only then command poetry run dj started working


Although the previous answers are correct, they are a bit complicated. The simplest way to run a python script with poetry is as follows:

poetry run python myscript.py

If you are using a dev framework like streamlit you can use

poetry run streamlit run myapp.py

Basically anything you put after poetry run will execute from the poetry virtual environment.