2021 年发布 Python 软件包的正确姿势!
podsearch
- 一种在iTunes中搜索播客的实用程序。让我们创建一个目录和一个虚拟环境:$ mkdir podsearch
$ cd podsearch
$ python3 -m venv env
$ . env/bin/activate
.
├── .gitignore
└── podsearch
└── __init__.py
"""Let's find some podcasts!"""
__version__ = "0.1.0"
def search(name, count=5):
"""Search podcast by name."""
raise NotImplementedError()
https://flit.readthedocs.io/en/latest/
)小程序可以简化所有操作。让我们安装它:pip install flit
$ flit init
Module name [podsearch]:
Author [Anton Zhiyanov]:
Author email [m@antonz.org]:
Home page [https://github.com/nalgeon/podsearch-py]:
Choose a license (see http://choosealicense.com/ for more info)
1. MIT - simple and permissive
2. Apache - explicitly grants patent rights
3. GPL - ensures that code based on this is shared with the same terms
4. Skip - choose a license later
Enter 1-4 [1]: 1
Written pyproject.toml; edit that file to add optional extra info.
pyproject.toml
pyproject.toml
- 项目元数据文件。它已经具有将程序包发布到公共存储库-PyPI所需的一切。TestPyPi
(测试存储库)和PyPI
(主要存储库)。它们是完全独立的,因此您将需要两个帐户。~/ .pypirc
中设置对存储库的访问权限:[distutils]
index-servers =
pypi
pypitest
[pypi]
username: nalgeon # replace with your PyPI username
[pypitest]
repository: https://test.pypi.org/legacy/
username: nalgeon # replace with your TestPyPI username
$ flit publish --repository pypitest
Found 4 files tracked in git
...
Package is at https://test.pypi.org/project/podsearch/
TestPyPi
上获得。
# ...
SEARCH_URL = "https://itunes.apple.com/search"
@dataclass
class Podcast:
"""Podcast metadata."""
id: str
name: str
author: str
url: str
feed: Optional[str] = None
category: Optional[str] = None
image: Optional[str] = None
def search(name: str, limit: int = 5) -> List[Podcast]:
"""Search podcast by name."""
params = {"term": name, "limit": limit, "media": "podcast"}
response = _get(url=SEARCH_URL, params=params)
return _parse(response)
flit publish
Readme
和变更日志changelog
README.md
和CHANGELOG.md
。README.md
CHANGELOG.md
pyproject.toml
,以便PyPI在软件包页面上显示它:description-file = "README.md"
requires-python = ">=3.7"
__init__.py
中的版本,并通过flit publish
发布软件包:Linters
和tests
black
),测试覆盖率(coverage
),代码质量(flake8
,pylint
,mccabe
)和静态分析(mypy
)。我们将通过tox
处理一切。$ pip install black coverage flake8 mccabe mypy pylint pytest tox
tox.ini
中创建tox
配置:[tox]
isolated_build = True
envlist = py37,py38,py39
[testenv]
deps =
black
coverage
flake8
mccabe
mypy
pylint
pytest
commands =
black podsearch
flake8 podsearch
pylint podsearch
mypy podsearch
coverage erase
coverage run --include=podsearch/* -m pytest -ra
coverage report -m
tox.ini
$ tox -e py39
...
py39 run-test: commands[0] | black podsearch
All done!
...
py39 run-test: commands[2] | pylint podsearch
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)
...
py39 run-test: commands[6] | coverage report -m
TOTAL 100%
...
py39: commands succeeded
congratulations :)
GitHub Actions
构建项目,使用Codecov
检查测试覆盖率,并使用Code Climate
检查代码质量。Codecov
和Code Climate
(均支持GitHub登录)并在设置中启用软件包存储库。.github / workflows / build.yml
:# ...
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8, 3.9]
env:
USING_COVERAGE: "3.9"
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: $
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install black coverage flake8 flit mccabe mypy pylint pytest tox tox-gh-actions
- name: Run tox
run: |
python -m tox
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
if: contains(env.USING_COVERAGE, matrix.python-version)
with:
fail_ci_if_error: true
build.yml
tox
进行测试。tox-gh-actions
软件包和USING_COVERAGE
设置可确保tox
使用与strategy.matrix
所需的 GitHub Actions 相同的 Python 版本。Codecov
。Code Climate
不需要单独的步骤-它会自动发现存储库更改。README.md
添加徽章:[![PyPI Version][pypi-image]][pypi-url]
[![Build Status][build-image]][build-url]
[![Code Coverage][coverage-image]][coverage-url]
[![Code Quality][quality-image]][quality-url]
...
<!-- Badges -->
[pypi-image]: https://img.shields.io/pypi/v/podsearch
[pypi-url]: https://pypi.org/project/podsearch/
[build-image]: https://github.com/nalgeon/podsearch-py/actions/workflows/build.yml/badge.svg
[build-url]: https://github.com/nalgeon/podsearch-py/actions/workflows/build.yml
[coverage-image]: https://codecov.io/gh/nalgeon/podsearch-py/branch/main/graph/badge.svg
[coverage-url]: https://codecov.io/gh/nalgeon/podsearch-py
[quality-image]: https://api.codeclimate.com/v1/badges/3130fa0ba3b7993fbf0a/maintainability
[quality-url]: https://codeclimate.com/github/nalgeon/podsearch-py
tox
很好,但对于开发来说不是很方便。运行单个命令(例如pylint
,coverage
等)的速度更快。但是它们非常冗长,因此我们将一些无意义的操作进行自动化处理。Makefile
的频繁操作创建简短的别名:.DEFAULT_GOAL := help
.PHONY: coverage deps help lint push test
coverage: ## Run tests with coverage
coverage erase
coverage run --include=podsearch/* -m pytest -ra
coverage report -m
deps: ## Install dependencies
pip install black coverage flake8 mccabe mypy pylint pytest tox
lint: ## Lint and static-check
flake8 podsearch
pylint podsearch
mypy podsearch
push: ## Push code with tags
git push && git push --tags
test: ## Run tests
pytest -ra
Makefile
$ make help
Usage: make [task]
task help
------ ----
coverage Run tests with coverage
deps Install dependencies
lint Lint and static-check
push Push code with tags
test Run tests
help Show help message
make
调用替换原始的build.yml
步骤:- name: Install dependencies
run: |
make deps
- name: Run tox
run: |
make tox
flit publish
。让我们创建一个单独的工作流程:name: publish
on:
release:
types: [created]
jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: "3.9"
- name: Install dependencies
run: |
make deps
- name: Publish to PyPi
env:
FLIT_USERNAME: ${{ secrets.PYPI_USERNAME }}
FLIT_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
make publish
publish.yml
Settings
> Secrets
> New repository secret
)中设置了PYPI_USERNAME
和PYPI_PASSWORD
。使用您的PyPi用户名和密码,甚至更好的-API令牌。pyproject.toml
tox.ini
Makefile
build.yml
publish.yml
更多阅读
特别推荐
点击下方阅读原文加入社区会员
评论