Search code examples
pythonpython-poetrypyproject.toml

How to add a shortcut to a custom script in pyproject.toml (using poetry)


I recently switched to Poetry from Pipenv. I'm used to having this section in my Pipfile:

[scripts]
test="pytest -s"
test:watch="ptw --runner 'pytest -s'"

so I can easily run my tests without typing out the full command or entering the shell, e.g.:

pipenv run test:watch

When I try something similar in pyproject.toml:

[tool.poetry.scripts]
watch = "ptw --runner 'pytest -s'"

I get an error:

$ poetry run watch

not enough values to unpack (expected 2, got 1)

Is there a different section in the pyproject.toml that I should be using for this?


Solution

  • [tool.poetry.scripts] entries in a Poetry pyproject.toml file do not serve the same purpose as [scripts] entries in Pipenv's Pipfile. Here is the documentation for Poetry's scripts. It is meant for console_scripts entry points, and the values should be of the form: importable.module:object.attr.

    What you might want to use to achieve your goal is something like poethepoet or any other similar "task runner" tool, for example: