24
  1. 我正在使用诗歌来创建 .whl 文件。
  2. 我有一个在远程主机上运行的 ftp 服务器。
  3. 我写了一个 python 脚本log_revision.py(路径保存在数据库中)。

目前,我每次运行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. 我在做什么,我该如何解决?

会给予任何帮助,谢谢!

4

4 回答 4

42

目前,这些[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()直接将某些内容传递给该方法。相反,您可以使用命令行参数并解析它们,例如使用 pythons argparse

于 2019-12-13T05:25:23.430 回答
4

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

poetry run python myscript.py

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

poetry run streamlit run myapp.py

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

于 2021-10-01T12:40:34.870 回答
2

修补这样的问题几个小时并找到了解决方案

我的任务是通过诗歌脚本启动 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 才开始工作

于 2021-06-18T18:56:25.787 回答
2

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

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

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

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

于 2021-12-29T17:59:51.680 回答