大家早上好
最近比较忙,所以更新频率有点低
希望大家多多见谅
今天要和大家分享的是Poetry
Poetry 是一个包管理和打包的工具
在 Python 中,对于初学者来说
打包系统和依赖管理是非常复杂和难懂的
即使对于经验丰富的开发者
一个项目总是要同时创建多个文件:
- `setup.py`
- `requirements.txt`
- `setup.cfg`
- `MANIFEST.in`
还有最新的 `Pipfile`
基于此,Poetry将所有的配置都放置在一个 toml 文件中,
这些配置包括:依赖管理、构建、打包、发布
Poetry的灵感来自于其他语言的一些工具:
composer(PHP) 和 cargo (Rust)
我开源的photoshop_python_api最近也更新使用Poetry去打包我的项目了
下面是photoshop_python_api的pyproject.toml的文件
[tool.poetry]
name = "photoshop-python-api"
version = "0.16.0"
description = "Python API for Photoshop."
homepage = "https://github.com/loonghao/photoshop-python-api"
repository = "https://github.com/loonghao/photoshop-python-api"
documentation = "https://photoshop-python-api.readthedocs.io/en/master/"
keywords = ["python-api, photoshop-python-api", "photoshop", "python"]
authors = ["longhao <hal.long@outlook.com>"]
license = "MIT"
readme = "README.md"
classifiers = [
"Development Status :: 4 - Beta",
"License :: OSI Approved :: MIT License",
"Operating System :: Microsoft :: Windows",
"Operating System :: Microsoft :: Windows :: Windows 10",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
]
packages = [
{ include = "src/photoshop" },
]
[tool.poetry.dependencies]
python = "^3.6"
comtypes = "1.1.7"
[tool.poetry.dev-dependencies]
black = "^21.5b1"
commitizen = "^2.17.8"
pre-commit = "^2.13.0"
sphinx = "^4.0.2"
sphinx-rtd-theme = "^0.5.2"
codecov = "^2.1.11"
pylint = "^2.8.2"
isort = "^5.8.0"
pytest = "^6.2.4"
flake8 = "^3.9.2"
mypy = "^0.812"
coverage = "^5.5"
pytest-cov = "^2.12.0"
[tool.commitizen]
name = "cz_conventional_commits"
version = "0.16.0"
tag_format = "v$version"
version_files = [
"pyproject.toml:version",
"src/photoshop/__version__.py"
]
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
[tool.black]
line-length = 88
target_version = ['py36']
include = '\.pyi?$'
exclude = '''
(
/(
\.eggs # exclude a few common directories in the
| \.git # root of the project
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
)/
| foo.py # also separately exclude a file named foo.py in
# the root of the project
)
'''
[tool.isort]
profile = "black"
atomic = true
include_trailing_comma = true
lines_after_imports = 2
lines_between_types = 1
use_parentheses = true
src_paths = ["src", "test"]
filter_files = true
known_first_party = "src"
# Enforce import section headers.
import_heading_future = "Import future modules"
import_heading_stdlib = "Import built-in modules"
import_heading_thirdparty = "Import third-party modules"
import_heading_firstparty = "Import local modules"
force_sort_within_sections = true
force_single_line = true
# All project unrelated unknown imports belong to third-party.
default_section = "THIRDPARTY"
skip_glob = "*/docs/conf.py"
Poetry的官网文档也写的很棒,通俗易懂
大家可以看看下面一个简单的演示http://mpvideo.qpic.cn/0bf2mmaaoaaakmaj6df3irqfay6da5rqabya.f10002.mp4?dis_k=4ff861a942f164ad0e6bd4575e0b7e46&dis_t=1657794502&vid=wxv_1901104481186938880&format_id=10002&support_redirect=0&mmversion=false
用Poetry还是十分舒服的,如果你有过手写setup.py的经历
就知道Poetry的好用了
结语
通过Poetry构建成.whl文件我们可以很容的分发我的python包
不管是对内或者对外都可以
而且变成了标准的pip包之后还有一个好处就是
我们可以很容易的通过rez-pipz去把它变成一个rez包
好了今天的分享就到这了
我们下期再见