Poetry 的使用
- 什么是poetry?
- 一款python 包的管理和打包工具。
- 作为包的管理工具, 我们怎么在项目中 应用呢?
方法1. (适用于已经存在的项目并想用poetry 进行管理, 从模块安装开始)
步骤1. poetry init.
在此过程中, poetry 会引导用户输入一些关于包的关键信息, 比如包名称, 包的dependency 模块。 参见下面详细log.
此过程会生成pyproject.toml 文件。
D:\python_poetry2>poetry init This command will guide you through creating your pyproject.toml config. Package name [python_poetry2]: pac3 Version [0.1.0]: Description []: Author [DESKTOP-7N9ITBC\mli <lmlm004@qq.com>, n to skip]: Invalid author string. Must be in the format: John Smith <john@example.com> Author [DESKTOP-7N9ITBC\mli <lmlm004@qq.com>, n to skip]: n License []: Compatible Python versions [^3.10]: Would you like to define your main dependencies interactively? (yes/no) [yes] yes You can specify a package in the following forms: - A single name (requests): this will search for matches on PyPI - A name and a constraint (requests@^2.23.0) - A git url (git+https://github.com/python-poetry/poetry.git) - A git url with a revision (git+https://github.com/python-poetry/poetry.git#develop) - A file path (../my-package/my-package.whl) - A directory (../my-package/) - A url (https://example.com/packages/my-package-0.1.0.tar.gz) Package to add or search for (leave blank to skip): fastapi Found 20 packages matching fastapi Showing the first 10 matches Enter package # to add, or the complete package name if it is not listed []: [ 0] fastapi [ 1] aioauth-fastapi [ 2] fastapi-manage [ 3] fastapi-oracle [ 4] fastapi-viewsets [ 5] myapi-fastapi [ 6] fastapi-utils [ 7] fastapi-crud [ 8] fastapi-misskey [ 9] fastapi-analytics [10] > 0 Enter the version constraint to require (or leave blank to use the latest version): Using version ^0.88.0 for fastapi Add a package (leave blank to skip): Would you like to define your development dependencies interactively? (yes/no) [yes] yes Package to add or search for (leave blank to skip): pytest Found 20 packages matching pytest Showing the first 10 matches Enter package # to add, or the complete package name if it is not listed []: [ 0] pytest [ 1] pytest123 [ 2] 131228_pytest_1 [ 3] pytest-pingguo-pytest-plugin [ 4] pytest-symbols [ 5] pytest-circleci [ 6] pytest-parallel [ 7] pytest-grpc [ 8] pytest-vnc [ 9] pytest-vcrpandas [10] > 0 Enter the version constraint to require (or leave blank to use the latest version): Using version ^7.2.0 for pytest Add a package (leave blank to skip): Generated file [tool.poetry] name = "pac3" version = "0.1.0" description = "" authors = ["Your Name <you@example.com>"] readme = "README.md" [tool.poetry.dependencies] python = "^3.10" fastapi = "^0.88.0" [tool.poetry.group.dev.dependencies] pytest = "^7.2.0" [build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api" Do you confirm generation? (yes/no) [yes] yes
步骤2. 在上面init 的过程中, poetry 并没有真正的去安装模块。 执行命令 poetry install 来创建virtualenv, 并且在virtual env 中安装模块。
Option Step: Poetry 更改国内安装源。 (49条消息) Python 包管理工具poetry配置国内PyPI镜像源_y_bccl27的博客-CSDN博客_poetry 源
Option Step: Poetry 会创建virtualenv, 如果你想更改virtual env 的路径, 可以用下面这些命令
poetry config --list cache-dir = "C:\\Code\\python-venv\\poetry-config" # 此路径是virtualenv 的默认路径, 想改就改
步骤3: 在步骤2中,poetry 已经创建了virtualenv, 那么怎么才能activate 这个virtualenv 呢? here U are.
poetry shell
Tips:
- 我们一般安装模块用 “pip install ***”, 如何让poetry 知道我的项目新加了模块呢?
# 方法1 poetry add TOML # 方法2 更改pyproject.toml 的 [tool.poetry.dependencies] 章节, 比如 [tool.poetry.dependencies] python = "^3.10" fastapi = "^0.88.0"
posted on 2022-12-07 17:59 MissLi12138 阅读(951) 评论(0) 编辑 收藏 举报