简体   繁体   English

如何使用命令行脚本的诗歌组织 python 项目

[英]How to organize a python project with poetry for a command line script

When doing a poetry init I got the following structure:在进行诗歌初始化时,我得到了以下结构:

/packagename
  __init__.py
  packagename.py
 /packagename
  /tests
   __init__.py
   test_packagename.py
pyproject.toml

which is fine for a package but I do not see how to make it fit a command line script.这对于 package 来说很好,但我不知道如何使它适合命令行脚本。 When I have a script like script.py with the following code structure:当我有一个像 script.py 这样的脚本时,它的代码结构如下:

In file script.py:在文件 script.py 中:

#!/usr/bin/python3

def main():
  print("Ok")

if __name__ == '__main__':
  main()

It is not intended to be used as a python module however, it may have dependencies and tests that are interesting to be handle with poetry.它不打算用作 python 模块,但是,它可能具有与诗歌一起处理的有趣的依赖项和测试。

In some example it is shown that with the following poetry syntax:在某些示例中,显示了以下诗歌语法:

[tool.poetry.scripts]
cli_script = 'script.py:main'

then one can call the script with:然后可以使用以下命令调用脚本:

poetry run cli_script

I am looking for some guideline on howto organize properly my poetry project for such usage.我正在寻找一些关于如何正确组织我的诗歌项目以供此类使用的指南。

I have looked for option for poetry init (like poetry init --script) for instance.例如,我已经寻找了诗歌 init 的选项(如诗歌 init --script)。 But it seems that kind of use case was not covered in the new/init poetry options.但似乎这种用例并未包含在 new/init 诗歌选项中。

By "poetry init" I guess you mean poetry new . “诗歌初始化”我猜你的意思是poetry new But also then your structure looks a bit weird.但是你的结构看起来也有点奇怪。 I would suggest the following structure:我建议采用以下结构:

packagename
├── packagename
│   ├── __init__.py
│   └── cli.py
├── tests
│   ├── __init__.py
│   └── test_packagename.py
└── pyproject.toml

The pyproject.toml looks like this: pyproject.toml看起来像这样:

[tool.poetry]
name = "packagename"
version = "0.1.0"
description = ""
authors = ["finswimmer <finswimmer@example.org>"]

[tool.poetry.scripts]
cli_script = "packagename.cli:main"

[tool.poetry.dependencies]
python = "^3.6"

[tool.poetry.dev-dependencies]
pytest = "^5.2"

[build-system]
requires = ["poetry>=1.0"]
build-backend = "poetry.masonry.api"

You cli.py as in your example:cli.py就像你的例子一样:

#!/usr/bin/python3

def main():
  print("Ok")

if __name__ == '__main__':
  main()

After a poetry install you can run poetry run cli_script . poetry install后,您可以运行poetry run cli_script

Alternatively you can run:或者,您可以运行:

poetry run python3 script.py

where:在哪里:

  • script.py is the name of the file having python code. script.py是具有 python 代码的文件的名称。
  • python3 is the python executable inside the poetry virtual environment. python3是诗歌虚拟环境中的 python 可执行文件。 This can be python as well, depending on the python version you have.这也可以是python ,具体取决于您拥有的 python 版本。 You may confirm the same using poetry run python3 -V or poetry run python -V , before executing the above command.在执行上述命令之前,您可以使用poetry run python3 -Vpoetry run python -V确认相同。

This command can be used in crontab as well to schedule simple scripts.此命令也可以在crontab中使用,以安排简单的脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM