如何在pyproject.toml中为自定义脚本添加快捷方式(使用poetry)

huangapple go评论88阅读模式
英文:

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]Poetrypyproject.toml 文件中的条目与 PipenvPipfile 中的 [scripts] 条目并不具有相同的目的。这里是 Poetryscripts 文档。它用于 console_scripts entry points,值应该采用以下形式:importable.module:object.attr

要实现您的目标,您可能想要使用类似 poethepoet 或其他类似的“任务运行器”工具,例如:

英文:

[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:

答案2

得分: -3

错误提示表明可能存在命令本身的问题。请检查命令是否正确引用,因为ptwpytest -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.

huangapple
  • 本文由 发表于 2023年4月13日 22:03:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定