合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
[TOC] > [home](https://python-poetry.org/) ## 概述 Poetry 是一个 Python 依赖管理和打包工具,它可以帮助你管理项目的依赖、虚拟环境和打包发布。建议给团队项目使用,Poetry 需要 Python 3.8+。它是多平台的,目标是使其在 Linux、macOS 和 Windows 上同样出色地运行。 Poetry 的优势: 1. 依赖解析更智能 2. 虚拟环境管理更方便 3. 打包发布更简单 4. 依赖锁定更可靠 ## 安装 ```shell // Windows (PowerShell) (Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | py - // Linux/MacOS curl -sSL https://install.python-poetry.org | python3 - ``` ## 语法 初始化项目 ``` # 在现有项目中初始化 poetry init # 创建新项目 poetry new 项目名称 ``` 依赖管理 ``` # 添加依赖 poetry add 包名 poetry add 包名==版本号 # 指定版本 poetry add 包名@latest # 最新版本 # 删除依赖 poetry remove 包名 # 更新依赖 poetry update # 更新所有包 poetry update 包名 # 更新指定包 ``` 虚拟环境 ``` # 创建虚拟环境 poetry env use python3.8 # 激活虚拟环境 poetry shell # 查看虚拟环境信息 poetry env info # 列出所有虚拟环境 poetry env list ``` 运行命令 ``` # 在虚拟环境中运行命令 poetry run python script.py poetry run pytest ``` 项目打包和发布 ``` # 构建项目 poetry build # 发布到 PyPI poetry publish ``` 依赖检查和锁定 ``` # 检查依赖 poetry check # 导出依赖到 requirements.txt poetry export -f requirements.txt --output requirements.txt # 安装依赖 poetry install # 安装所有依赖 poetry install --no-dev # 只安装生产环境依赖 ``` 配置管理 ``` # 查看配置 poetry config --list # 设置配置 poetry config virtualenvs.in-project true # 将虚拟环境创建在项目目录下 ```