如何使用 pyproject.toml 设置和诗歌运行脚本?

hel*_*per 21 python python-poetry

  1. 我正在使用诗歌来创建 .whl 文件。
  2. 我有一个在远程主机上运行的 ftp 服务器。
  3. 我编写了一个 python 脚本 ( log_revision.py),它在数据库中保存了 git 提交、更多参数,最后将 .whl(创建的诗歌)发送到远程服务器(每个 .whl 在服务器中的不同路径中,路径保存在数据库中)。

目前我在每次运行推荐后手动运行脚本poetry build。我知道pyproject.toml有, [tool.poetry.scripts]但我不知道如何使用它来运行 python 脚本。

我试过

[tool.poetry.scripts]
my-script = "my_package_name:log_revision.py
Run Code Online (Sandbox Code Playgroud)

然后,poetry run my-script但我总是得到一个错误 AttributeError: module 'my_package_namen' has no attribute 'log_revision'

1.有人可以帮我理解如何运行许愿表吗?

作为一个短期选项(没有 git 和 params)我尝试使用poetry publish -r http://192.168.1.xxx/home/whl -u hello -p world但我收到以下错误

[RuntimeError]                                 
Repository http://192.168.1.xxx/home/whl is not defined  
Run Code Online (Sandbox Code Playgroud)

2. 我在做什么,我该如何解决?

会appricate任何帮助,谢谢!

fin*_*mer 33

目前这些[tool.poetry.scripts]部分相当于 setuptools console_scripts

因此参数必须是有效的模块和方法名称。让我们想象在你的包中my_package,你有log_revision.py,它有一个方法start()。然后你必须写:

[tool.poetry.scripts]
my-script = "my_package.log_revision:start"
Run Code Online (Sandbox Code Playgroud)

这是一个完整的例子:

你应该有这个文件夹结构:

my_package
??? my_package
?   ??? __init__.py
?   ??? log_revision.py
??? pyproject.toml
Run Code Online (Sandbox Code Playgroud)

内容pyproject.toml为:

[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"
Run Code Online (Sandbox Code Playgroud)

log_revision.py

def start():
    print("Hello")
Run Code Online (Sandbox Code Playgroud)

运行poetry install一次后,您应该能够执行以下操作:

$ poetry run my-script  
Hello
Run Code Online (Sandbox Code Playgroud)

您不能start()直接将某些内容传递给该方法。相反,您可以使用命令行参数并解析它们,例如使用 pythons argparse

  • 有没有办法将一些数据发送到函数 ```log_revision:start(arg1, arg2)```? (5认同)
  • 如何进行设置,以便可以直接运行命令,就像使用带有 setuptools 的 `flask run my_app` 一样,具有 `entry_points={"console_scripts": ["superset=superset.cli:superset"]}` (2认同)

Dav*_*veR 32

虽然前面的答案都是正确的,但是有点复杂。使用诗歌运行 python 脚本的最简单方法如下:

poetry run python myscript.py
Run Code Online (Sandbox Code Playgroud)

如果您正在使用像这样的开发框架,streamlit您可以使用

poetry run streamlit run myapp.py
Run Code Online (Sandbox Code Playgroud)

基本上你放在后面的任何内容poetry run都会从poetry virtual environment.


Nat*_*Nat 24

对于未来的访问者,我认为 OP 所要求的(构建后挂钩?)并没有直接支持。但是,您可能会对使用我编写的名为pothepoet 的工具感到满意,该工具与诗歌集成,以 shell 命令或引用 python 函数来运行 pyproject.toml 中定义的任意任务。

例如,您可以在 pyproject.toml 中定义类似以下内容

[tool.poe.tasks.log_revision]
script = "my_package.log_revision:main" # where main is the name of the python function in the log_revision module
help   = "Register this revision in the catalog db"

[tool.poe.tasks.build]
cmd  = "poetry build"
help = "Build the project"

[tool.poe.tasks.shipit]
sequence = ["build", "log_revision"]
help     = "Build and deploy"
Run Code Online (Sandbox Code Playgroud)

然后使用 poe CLI 执行任务,如下所示,这将按顺序运行其他两个任务,从而一次性构建您的项目并运行部署脚本!

[tool.poe.tasks.log_revision]
script = "my_package.log_revision:main" # where main is the name of the python function in the log_revision module
help   = "Register this revision in the catalog db"

[tool.poe.tasks.build]
cmd  = "poetry build"
help = "Build the project"

[tool.poe.tasks.shipit]
sequence = ["build", "log_revision"]
help     = "Build and deploy"
Run Code Online (Sandbox Code Playgroud)

默认情况下,任务在诗歌管理的 virtualenv 内执行(如使用poetry run),因此 python 脚本可以使用 dev 依赖项。

如果您需要定义 CLI 参数或将值从 dotenv 文件(例如凭据)加载到任务中,那么也支持此操作。


更新:诗歌插件支持

诗人现在可以在用作诗歌插件时支持后期构建挂钩。例如,当使用诗歌 >=1.2.0b1 时,您可以配置以下内容以在poetry build运行后自动运行 log_revision 任务:

[tool.poe.poetry_hooks]
post_build = "log-revision"

[tool.poe.tasks.log-revision]
script = "scripts:log_revision"
Run Code Online (Sandbox Code Playgroud)


小智 6

遇到这样的问题,研究了几个小时,找到了解决办法

\n

我的任务是通过诗歌脚本启动 django 服务器。

\n

这是目录。manage.py 位于测试文件夹中:

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 pyproject.toml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 README.rst\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 runserver.py\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 test\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 db.sqlite3\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 manage.py\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 asgi.py\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __pycache__\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.cpython-39.pyc\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 settings.cpython-39.pyc\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 urls.cpython-39.pyc\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 wsgi.cpython-39.pyc\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 settings.py\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 urls.py\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 wsgi.py\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 tests\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test_tmp.py\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 tmp\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 __init__.py\n
Run Code Online (Sandbox Code Playgroud)\n

我必须创建一个文件 runserver.py:

\n
 import subprocess                                                               \n                                                                                   \n  def djtest():                                                                   \n      cmd =['python', 'test/manage.py', 'runserver']                                                                 \n      subprocess.run(cmd)                                                    \n
Run Code Online (Sandbox Code Playgroud)\n

然后编写脚本本身pyproject.toml:

\n
[tool.poetry.scripts]                                                           \ndj = "runserver:djtest"  \n
Run Code Online (Sandbox Code Playgroud)\n

仍然对 pyproject.toml 进行更改:

\n
[tool.poetry.scripts]                                                           \ndj = "runserver:djtest"\n
Run Code Online (Sandbox Code Playgroud)\n

然后命令诗歌运行 dj 开始工作

\n

  • 你知道通过 subproc 调用 python 是否调用了你的诗歌 venv 或者使用了系统? (2认同)
  • 回答我自己的评论,是的,它拿起了诗歌 venv (2认同)