英文:
How to add a shortcut to a custom script in pyproject.toml (using poetry)
问题
[tool.poetry.scripts]
watch = "ptw --runner 'pytest -s'"
英文:
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?
答案1
得分: 5
[tool.poetry.scripts]
在 Poetry 的 pyproject.toml
文件中的条目与 Pipenv 的 Pipfile
中的 [scripts]
条目并不具有相同的目的。这里是 Poetry 的 scripts
文档。它用于 console_scripts
entry points,值应该采用以下形式:importable.module:object.attr
。
要实现您的目标,您可能想要使用类似 poethepoet 或其他类似的“任务运行器”工具,例如:
- chuy(另请参阅:poetry-chuy-plugin)
- doit
- invoke(另请参阅:poetry-pyinvoke-plugin)
- poethepoet
- poetry-exec-plugin
- taskipy
- thx
英文:
[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:
- chuy (see also: poetry-chuy-plugin)
- doit
- invoke (see also poetry-pyinvoke-plugin)
- poethepoet
- poetry-exec-plugin
- taskipy
- thx
答案2
得分: -3
错误提示表明可能存在命令本身的问题。请检查命令是否正确引用,因为ptw
和pytest -s
应被视为单独的参数。尝试将您的脚本定义更改为:
(C#)
[tool.poetry.scripts] watch = "ptw --runner 'pytest -s'"
(Swift)
[tool.poetry.scripts] watch = "ptw --runner \"pytest -s\""
英文:
The error you're seeing suggests that there may be an issue with the command itself. One thing to check is if the command is correctly quoted, since ptw
and pytest -s
should be treated as separate arguments. Try changing your script definition to:
(c#)
[tool.poetry.scripts] watch = "ptw --runner 'pytest -s'"
To
(Swift)
[tool.poetry.scripts] watch = "ptw --runner \"pytest -s\""
and see if that resolves the error.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论