官网: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/bin
Windows
系统 - 在%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
-
重点关注
- cache-dir - 缓存目录,默认即可
- virtualenvs.create = true - 默认为true。当执行poetry install或poetry add命令时,若当前没有虚拟环境则会自动创建;为false时会自动安装到系统环境
- virtualenvs.in-project = null - 默认为null。设置为true时,会在当前项目目录下创建虚拟环境
- virtualenv.path - 虚拟环境的存储路径
-
更改配置 -
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与django联合
-
安装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 <xxx@xiaomi.com>"] [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