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

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

  1. 我正在使用诗歌创建 .whl 文件。
  2. 我在远程主机上有一个 ftp 服务器 运行ing。
  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

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

1.有人可以帮助我了解如何 运行 赞扬吗?

作为短期选项(没有 git 和参数)我尝试使用 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  

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

愿意提供任何帮助,谢谢!

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

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

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

这是一个完整的例子:

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

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

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"

log_revision.py:

def start():
    print("Hello")

在你拥有 运行 poetry install 之后你应该能够做到这一点:

$ poetry run my-script  
Hello

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

解决这个问题几个小时,找到了解决方案

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

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

├── 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

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

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

然后自己写脚本pyproject.toml:

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

并且仍然对 pyproject.toml 进行更改:

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

才指挥诗歌运行dj开始工作

前面的答案虽然都对,但是有点复杂。 运行一个python有诗的脚本最简单的方法如下:

poetry run python myscript.py

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

poetry run streamlit run myapp.py

基本上你放在 poetry run 之后的任何东西都会从 poetry virtual environment.

开始执行

对于未来的访问者,我认为不直接支持 OP 要求的内容(post 构建挂钩?)。但是您可能会对使用我编写的名为 poethepoet 的工具感到满意,该工具将诗歌与诗歌集成到 pyproject.toml 中根据 shell 命令或通过引用 pyproject.toml 中定义的任意任务 运行 =34=] 函数。

例如,您可以在 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"

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

poe shipit

默认情况下任务在 poetry 管理的 virtualenv 中执行(比如使用 poetry run)所以 python 脚本可以使用开发依赖。

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


更新:诗歌插件支持

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

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

[tool.poe.tasks.log-revision]
script = "scripts:log_revision"