
Poetry CLI命令大全50常用命令速查清单【免费下载链接】poetryPython packaging and dependency management made easy项目地址: https://gitcode.com/GitHub_Trending/po/poetryPoetry 是 Python 生态中最流行的打包与依赖管理工具之一。这份Poetry CLI 命令速查清单按使用场景整理了 50 条常用命令与参数组合覆盖新建项目、添加依赖、虚拟环境、构建发布、配置源等全部日常操作配合表格与简短示例方便你 1 分钟查到想要的命令。 完整官方文档见 docs/cli.md命令的完整源码注册表在 src/poetry/console/application.py。 快速导航你的需求直达章节从零开始建一个项目快速开始装/删/升级依赖依赖管理在虚拟环境里跑代码运行与环境打包、发版、改版本号构建与发布换源、清缓存、改配置配置与源排错、插件、升级自身调试与进阶 快速开始poetry new 与 init开始项目有两条路new从零脚手架一个新包init在已有目录里交互式生成pyproject.toml。命令作用示例poetry new新建项目默认 src 布局poetry new my-packagepoetry new --flat使用扁平布局创建项目poetry new --flat my-packagepoetry new --name目录名与包名不一致时使用poetry new my-folder --name my-packagepoetry init在当前目录交互式创建pyproject.tomlpoetry init下图是在终端中创建并运行 Poetry 演示项目的动画可以直观感受整个流程新建完成后my-package目录里会自动生成pyproject.toml、README.md、src/和tests/后续所有管理都围绕pyproject.toml与poetry.lock两个文件展开详见 docs/pyproject.md。 依赖管理日常高频命令这是使用频率最高的一组命令建议重点记忆命令作用示例poetry add添加依赖并写入pyproject.toml、安装poetry add requests pendulumpoetry add 版本约束指定版本范围添加poetry add pendulum^2.0.5poetry add -G添加到指定依赖组poetry add mkdocs --group docspoetry add -D作为开发依赖添加-G dev的简写poetry add -D pytestpoetry add -e以可编辑模式添加本地/路径/VCS 依赖poetry add --editable ./my-package/poetry add extras安装依赖的可选功能poetry add requests[security,socks]poetry remove从项目中移除依赖poetry remove pendulumpoetry remove -G从指定依赖组移除poetry remove mkdocs --group docspoetry update按约束刷新依赖并更新锁文件poetry updatepoetry update 包名只更新部分包poetry update requests tomlpoetry update --lock只刷新锁文件不执行安装poetry update --lockpoetry install按poetry.lock安装全部依赖poetry installpoetry install --no-root跳过安装项目自身poetry install --no-rootpoetry install --without排除指定依赖组poetry install --without test,docspoetry install -E安装指定 extraspoetry install -E mysql -E pgsqlpoetry install --all-groups安装所有依赖组含可选组poetry install --all-groupspoetry sync与锁文件严格同步多装的包会被移除poetry syncpoetry lock只锁依赖、不安装poetry lockpoetry lock --regenerate从头重新生成锁文件poetry lock --regeneratepoetry show列出已安装的全部包poetry showpoetry show 包名查看某个包的版本与依赖关系poetry show pendulumpoetry show --tree以树形展示依赖树poetry show --treepoetry show --outdated只显示可升级的包poetry show -opoetry show --why显示包被谁依赖排查幽灵依赖poetry show --whypoetry show -T只显示直接依赖poetry show --top-level 三个命令的关系一句话总结add/remove改声明pyproject.toml→lock固定版本 →install/sync装到环境。更多细节见 docs/managing-dependencies.md。️ 运行代码与虚拟环境管理poetry run让命令跑在项目虚拟环境中env系列管理多个虚拟环境python系列管理 Python 版本2.1.0 起的实验特性命令作用示例poetry run在虚拟环境中执行任意命令poetry run python -Vpoetry run 脚本名执行pyproject.toml中定义的脚本poetry run my-scriptpoetry env use为项目切换/创建 Python 版本环境poetry env use 3.12poetry env info查看当前环境信息poetry env infopoetry env info -p只显示虚拟环境路径poetry env info --pathpoetry env list列出项目所有虚拟环境poetry env listpoetry env remove移除指定虚拟环境poetry env remove 3.11poetry env remove --all移除项目全部虚拟环境poetry env remove --allpoetry env activate打印激活当前环境的 shell 命令poetry env activatepoetry python install安装指定 Python 版本poetry python install 3.12poetry python list查看系统/Poetry 管理的全部 Pythonpoetry python list --allpoetry python remove移除 Poetry 管理的 Python 版本poetry python remove 3.12 虚拟环境的更多用法激活、清理见 docs/managing-environments.md。️ 构建、发布与版本管理命令作用示例poetry build构建 sdist 与 wheel 发布包poetry buildpoetry build -f只构建指定格式poetry build --format wheelpoetry publish发布dist/中的包到仓库poetry publishpoetry publish --build先构建再发布poetry publish --buildpoetry publish --dry-run演练发布不真正上传poetry publish --dry-runpoetry version查看当前项目版本poetry versionpoetry version 规则按规则升版并写回pyproject.tomlpoetry version minorpoetry version --short只输出版本号适合脚本poetry version --shortpoetry check校验pyproject.toml与锁文件一致性poetry checkpoetry check --strict出现警告也判定为失败适合 CIpoetry check --strict升版规则支持major/minor/patch及premajor、preminor、prepatch、prerelease等预发布规则。仓库源与凭据的配置见 docs/repositories.md。⚙️ 配置、软件源与缓存管理命令作用示例poetry config --list列出所有配置项poetry config --listpoetry config 键值设置配置poetry config virtualenvs.create falsepoetry config --local写入项目级配置poetry.tomlpoetry config --local cache-dir ~/.cachepoetry config --unset删除某条配置poetry config --unset virtualenvs.createpoetry source add添加包源可设优先级poetry source add --priority primary my-repo https://index.example.com/simplepoetry source show查看项目配置的源poetry source showpoetry source remove移除一个源poetry source remove pypi-testpoetry cache list列出所有缓存poetry cache listpoetry cache clear清理缓存支持按源、按包poetry cache clear --all配置项的完整说明见 docs/configuration.md。 调试、排错与 Poetry 自管理命令作用示例poetry about查看 Poetry 与 poetry-core 版本信息poetry aboutpoetry list列出全部可用命令poetry listpoetry help全局帮助poetry helppoetry help 命令名查看单条命令帮助poetry help showpoetry debug info输出 Poetry 与环境调试信息poetry debug infopoetry debug resolve复现依赖解析定位解析报错poetry debug resolvepoetry debug tags查看当前环境支持的打包标签poetry debug tagspoetry search在远程索引搜索包poetry search requestspoetry self add给 Poetry 自身装插件poetry self add poetry-plugin-exportpoetry self remove移除 Poetry 插件poetry self remove poetry-plugin-exportpoetry self update升级 Poetry 本体poetry self updatepoetry self show查看 Poetry 运行时安装的包poetry self showpoetry self show plugins列出已安装插件poetry self show pluginspoetry self sync同步 Poetry 运行时环境poetry self syncpoetry self lock重新锁定 Poetry 自身依赖poetry self lock⚠️ 注意export、shell等旧命令在 2.0 起不再内置需通过poetry self add安装对应插件插件机制见 docs/plugins.md。 10 个全局通用小技巧以下参数几乎适用于所有命令值得记在脑子里参数作用-v/-vv/-vvv递增详细程度-vvv输出调试信息-q静默模式不输出任何消息-n/--no-interaction不交互CI 中常用-C DIR指定工作目录执行命令-P DIR指定另一个项目根目录--no-plugins本次禁用插件--no-cache本次禁用缓存冷缓存排障--dry-run只打印将执行的操作不实际执行--lock只更新锁文件不安装SHELL_VERBOSITY环境变量在 CI/脚本中代替-v控制详细度更多命令选项可对照官方文档 docs/cli.md各命令的实现位于 src/poetry/console/commands/ 目录需要深入了解某条命令的行为时可按名查找。❓ 高频疑问速答1.install和sync有什么区别install按锁文件安装、不会移除多余包sync会严格同步把锁文件里没有的包删掉。日常推荐优先sync在系统环境安装的场景除外。2.update为什么不升级某些包update不会修改pyproject.toml中的版本约束只会在已有约束内取最新版本。想放宽约束请用poetry add 包名^新版本。3. 提示某个命令不存在先跑poetry self show plugins确认插件是否安装2.0 之后部分命令如export、shell已迁移到独立插件。4. 怎么快速定位这个包是谁引进来的poetry show --why或poetry show 包名 --tree一行搞定。 命令用熟之后可以继续学习 docs/managing-dependencies.md 中的依赖组groups与 extras 玩法它们配合--with/--without/--only参数能让install和sync的粒度更精准。【免费下载链接】poetryPython packaging and dependency management made easy项目地址: https://gitcode.com/GitHub_Trending/po/poetry创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考