Python包管理工具之Poetry

今天给大家介绍一个poetry工具,它是一个Python 虚拟环境和依赖管理工具。还提供了打包发布的
首页 新闻资讯 行业资讯 Python包管理工具之Poetry

636c8346082791920b2963c1db0df6b7fdbf76.png

前言

之前讲了一个pipenv包管理工具,这里说另外一个poetry工具,它是一个Python 虚拟环境和依赖管理工具。还提供了打包发布的功能。

一、poetry安装

MacOS下使用官网推荐方式安装,安装路径是:$HOME/.poetry/bin。

复制

curl -sSL https://install.python-poetry.org | python -
  • 1.

有与网络问题可能会导致Timeout的问题,可以参考这篇文章解决:

https://www.ayunw.cn/posts/terminal_configure_proxy_with_clash/。

如果以上都不能解决,还可以用pipx工具安装:

复制

# MacOS安装pipx工具brew install pipxpipx ensurepath# 根据以下命令提示设置pipx的shell补全pipx completions# pipx安装poetry❯ pipx install poetry❯ poetry --versionPoetry version 1.1.13# 它会自动添加环境变量到~/.zshrc中# pipx升级poetrypipx upgrade poetry# pipx卸载poetrypipx uninstall poetry# 设置命令补全❯ mkdir $ZSH_CUSTOM/plugins/poetry❯ poetry completions zsh > $ZSH_CUSTOM/plugins/poetry/_poetry❯ vi ~/.zshrcplugins(  poetry
  ...
  )  
❯ source ~/.zshrc
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

二、使用poetry

创建项目

复制

poetry new demo1demo1├── pyproject.toml├── README.rst├── demo1│   └── __init__.py└── tests├── __init__.py└── test_demo1.py
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

创建虚拟环境

复制

cd demo1
poetry install
  • 1.

  • 2.

设置Pypi的源

在 pyproject.toml 文件末尾追加以下内容:

复制

[[tool.poetry.source]]name = "aliyun"url = "http://mirrors.aliyun.com/pypi/simple"default = true
  • 1.

  • 2.

  • 3.

  • 4.

激活使用虚拟环境

复制

# 不激活虚拟环境使用命令poetry run python start.py# 激活虚拟环境使用命令poetry shell# 安装包poetry add flask# 查看安装的所有包poetry show# 查看单个包poetry show flask# 卸载包poetry remove flask# 退出虚拟环境exit# 查看虚拟环境路径poetry env info --path# 删除虚拟环境poetry env remove /full/path/to/python
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

三、Pycharm使用Poetry

Pycharm中,poetry是以一个插件的形式存在的,所以首先打开Pycharm需要安装插件。

安装完成后新建项目,就可以选择使用Poetry了。

15    2022-07-01 09:17:14    Python poetry工具