官网:https://python-poetry.org/docs/
Poetry是Python中用于依赖管理和打包的工具。它允许您声明项目所依赖的库,并将为您管理(安装/更新)它们。
**不推荐使用
pip install --user poetry**安装安装前需先安装
Python环境以下命令中管道符后需替换到指定python版本
OSX/Linux系统 - 直接在终端安装curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
Windows系统 - 建议在powershell安装(Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python -
安装完后,
poetry会在bin下自动生成可执行程序。
OSX/Linux系统 - 在$HOME/.poetry/binWindows系统 - 在%USERPROFILE%\.poetry\bin校验是否安装成功:
❯ poetry --version
Poetry version 1.1.14
poetry self update
poetry self update --preview # 安装的是 `pre-release`版本
poetry self update 0.8.0 # 更新到指定版本
curl -sSL https://install.python-poetry.org | python3 - --uninstall
curl -sSL https://install.python-poetry.org | POETRY_UNINSTALL=1 python3 -
以下以已存在的仓库项目为例
mkdir ibn-quality-be && cd ibn-quality-be
poetry配置poetry init
# 根据提示完善信息,或一路回车后在`pyproject.toml`文件中维护
poetry安装源[[tool.poetry.source]]
name = 'aliyun.mirrors'
url = "http://mirrors.aliyun.com/pypi/simple/"
查看配置 - poetry config --list
重点关注
更改配置 - poetry config 配置名 配置值
# 默认习惯,将虚拟环境创建在项目目录下,可统一管理
poetry config virtualenvs.in-project true
| 命令 | 含义 | 备注 |
|---|---|---|
| poetry new xxx | 创建新的python项目 | 一般建议在已存在的工程下使用poetry init来初始化 |
| poetry init | 在已存在的工程下初始化 | 会自动生成pyproject.toml文件 |
| poetry install xxx | 根据pyproject.toml文件配置安装依赖 | |
| poetry update | 更新依赖 | |
| poetry add xxx | 添加xxx进pyproject.toml文件,同时安装依赖 | 若一次安装多个依赖,则用空格分隔,如:poetry add a b c 支持指定版本安装,可见:https://python-poetry.org/docs/master/cli/#add |
| poetry remove xxx | 删除依赖 | |
| poetry show xxx | 查看可用的包信息 | |
| poetry build | 构建并归档 | |
| poetry publish | 发布到远程存储库 | |
| poetry config --list | 查看配置 |
poetry shell,会自动生效当前项目的虚拟环境# 会自动调用虚拟环境下的`activate`脚本生效
❯ poetry shell
Spawning shell within /Users/xxx/Work/projects/xiaomi/ibn-quality/ibn-quality-be/.venv
❯ . /Users/xxx/Work/projects/xiaomi/ibn-quality/ibn-quality-be/.venv/bin/activate
source {path_to_venv}/bin/activate ,powershell用户执行source {path_to_venv}\Scripts\activate.ps1查看项目虚拟环境路径:poetry env info --path
poetry shell,则直接执行exit即可退出虚拟环境deactivate退出虚拟环境安装poetry - 见上文
更改poetry配置:poetry config virtualenvs.in-project true
创建项目目录:mkdir ibn-quality-be && cd ibn-quality-be
初始化poetry:poetry init,可一路回车
修改项目基础配置 - pyproject.toml
[tool.poetry]
name = "ibn-quality-be"
version = "0.1.0"
description = "xx区质量委质量后台服务"
authors = ["xxx "]
[tool.poetry.dependencies]
python = "^3.10"
[tool.poetry.dev-dependencies]
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
[[tool.poetry.source]]
name = 'aliyun.mirrors'
url = "https://mirrors.aliyun.com/pypi/simple/"
添加依赖,如django/djangorestframework - poetry add django djangorestframwork
激活虚拟环境 - poetry shell
新建django项目 - django-admin startproject ibn_quality_be .(注意最后有一个点,表示在当前目录下创建)
创建apps目录 - 应用程序统一目录管理 - cd ibn-quality-be && mkdir apps
创建应用程序 - cd apps && django-admin startapp message_template
# 最终项目目录如下:/Users/xxx/Work/projects/xiaomi/ibn-quality/ibn-quality-be
❯ tree ibn-quality-be -I .venv
ibn-quality-be
├── apps
│ └── message_template
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── ibn_quality_be
│ ├── __init__.py
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
├── poetry.lock
└── pyproject.toml
4 directories, 15 files