TOC

Poetry

我研究了半天之后感觉无法掌握 poetry,决定放弃,还是用我的 Pipenv。

img

Poetry 是一个 Python 虚拟环境管理器。

安装

# pip install poetry

# 官网推荐的安装方法
curl -sSL https://install.python-poetry.org | python3 -

# 也可以用 pipx (另一种推荐方法)
pipx install poetry

官网推荐的方法会安装在虚拟环境中,避免依赖冲突。

项目初始化

# 为项目创建 pyproject.toml
# poetry init [--name <...>] [--description <...>] [--author <...>] [--python <...>] [--dependency <...>] [--dev-dependency <...>] [-l <...>]
# 不指定参数,使用默认值(自动判断)
poetry init

cat pyproject.toml
# [tool.poetry]
# name = "markjour"
# version = "0.1.0"
# description = ""
# authors = ["markjour <9527@markjour.com>"]

# [tool.poetry.dependencies]
# python = "^3.9"

# [tool.poetry.dev-dependencies]

# [build-system]
# requires = ["poetry-core>=1.0.0"]
# build-backend = "poetry.core.masonry.api"
$ poetry new newProject
Created package newproject in newProject

$ tree newProject
newProject
├── newproject
│   └── __init__.py
├── pyproject.toml
├── README.rst
└── tests
    ├── __init__.py
    └── test_newproject.py

2 directories, 5 files

$ bat newProject/pyproject.toml
───────┬───────────────────────────────────────────────────────────────────────
       │ File: newProject/pyproject.toml
───────┼───────────────────────────────────────────────────────────────────────
   1   │ [tool.poetry]
   2   │ name = "newProject"
   3   │ version = "0.1.0"
   4   │ description = ""
   5   │ authors = ["markjour <9527@markjour.com>"]
   6   │
   7   │ [tool.poetry.dependencies]
   8   │ python = "^3.9"
   9   │
  10   │ [tool.poetry.dev-dependencies]
  11   │ pytest = "^5.2"
  12   │
  13   │ [build-system]
  14   │ requires = ["poetry-core>=1.0.0"]
  15   │ build-backend = "poetry.core.masonry.api"
───────┴───────────────────────────────────────────────────────────────────────

配置

默认配置:

# poetry config --list
cache-dir = "/home/markjour/.cache/pypoetry"
experimental.new-installer = true
installer.parallel = true
virtualenvs.create = true
virtualenvs.in-project = null
virtualenvs.path = "{cache-dir}/virtualenvs"  # /home/markjour/.cache/pypoetry/virtualenvs

文件位置: ~/.config/pypoetry/config.toml

poetry 的一个严重问题是它不会像其他环境和依赖管理工具一样使用 pip 的配置。
需要重新为 poetry 配置一遍。

我习惯使用的 pip 特性:指定 index 路径,下载,find-links。

# 用户定义配置会写入本地文件
poetry config repositories.douban https://pypi.doubanio.com/simple
poetry config repositories.aliyun https://mirrors.aliyun.com/pypi/simple/

实践

[[tool.poetry.source]]¬
name = "aliyun"¬
default = true¬
url = "https://mirrors.aliyun.com/pypi/simple/"¬

Issue #1632 (Allow user to override PyPI URL without modifying pyproject.toml)
2019-11-26 提出来的,到现在都没有 Close。

有几个人提交修改,似乎都还没有合并到 master。