
Python世界中,Poetry是一个近年来备受瞩目的工具,它为开发者提供了一个灵活且强大的依赖管理解决方案。Poetry可以帮助开发者管理项目的依赖关系,同时提供了一系列的工具和功能,使开发者能够更轻松地创建和管理复杂的项目。在这篇文章中,我们将深入了解Poetry的基本概念、特点和优势。
Poetry是一个Python的依赖管理工具,它旨在提供一种更简单、更直观的方式来管理Python项目的依赖关系。与传统的pip工具相比,Poetry具有更多的功能和优势,如自动解决依赖冲突、创建虚拟环境、管理项目配置等。
以下是Poetry的一些主要特点和优势:
install : curl -sSL https://install.python-poetry.org | python3 -
add env : export PATH="/Users/chendongsheng/.local/bin:$PATH"
给on-myzsh添加自动完成
mkdir $ZSH_CUSTOM/plugins/poetry
poetry completions zsh > $ZSH_CUSTOM/plugins/poetry/_poetry
plugins(
poetry
...
)
新建一个项目: poetry new poetry-demo
pyproject.toml: 文件是最重要的初始化已经存在的项目:
cd pre-existing-project
poetry init
# 使用poetry配置文件的python 环境,默认放在.venv的文件夹中
poetry env use python
# 进入当前虚拟环境
poetry shell
# 退出虚拟环境
exit
# 添加新的依赖项目
poetry add flask
# 现实当前的项目list
poetry show
poetry show --tree
# 移除依赖
poetry remve xxx
# 使用poetry的python环境进行执行
poetry run python your_script.py
# 安装已经有的poetry环境
poetry install
# 将black新增到dev分支
poetry add black --group dev
poetry add pytest --group test
# 安装所有组的依赖
poetry install
# 安装 main 组的依赖 (三种写法)
poetry install --with main
poetry install --only main
poetry install --only-root
# 选择安装哪些组 (--without优先级高于with)
poetry install --with test,docs --without docs
# 将分组内的依赖都删除
poetry remove mkdocs --group docs
# 同步poetry的依赖
poetry install --sync
# 选择性同步poetry的某一个组
poetry install --without dev --sync
poetry install --with docs --sync
poetry install --only dev
配置路径
Linux: $XDG_CONFIG_HOME/pypoetry or ~/.config/pypoetry
Windows: %APPDATA%\pypoetry
MacOS: ~/Library/Application Support/pypoetry
数据文件夹
$XDG_DATA_HOME/pypoetry or ~/.local/share/pypoetry%APPDATA%\pypoetry~/Library/Application Support/pypoetry缓存文件夹
$XDG_CACHE_HOME/pypoetry or ~/.cache/pypoetry%LOCALAPPDATA%\pypoetry~/Library/Caches/pypoetry# 查看相关的配置
poetry config --list
# 将xxx.xxx改为true
poetry config xxx.xxx true
poetry config virtualenvs.path /path/to/cache/directory/virtualenvs
poetry config virtualenvs.path --unset
poetry source add --priority=supplemental foo https://pypi.example.org/simple/
# 新加源foo
poetry source add foo https://foo.bar/simple/
# 将foo源设置为默认源
poetry source add --priority=default foo https://foo.bar/simple/
# 设置使用哪个虚拟环境的python
poetry env use /full/path/to/python
poetry env use python3.7
# 显示环境
poetry env info
# 获取环境路径
poetry env info --path
# 获取python可执行路径
poetry env info --executable
# 获取当前环境的所有依赖
poetry env list
# 删除环境
poetry env remove /full/path/to/python
poetry env remove python3.7
poetry env remove 3.7
poetry env remove test-O3eWbxRl-py3.7
poetry add django@^4.0.0
poetry add django@latest
需要安装插件: Export Poetry Plugin
poetry self add poetry-plugin-export
poetry export -f requirements.txt --without-hashes --output requirements.txt
# 插件安装
poetry self add poetry-plugin
# 插件卸载
poetry self remove poetry-plugin
