RGS*_*RGS 6 python packaging package
创建 Python 包时,您只需编写代码、构建包并在 PyPI 上共享即可。但是,你是怎么做的?
然后,如果你想更进一步怎么办?
当您发布了数十个包后,您就知道如何以适合您的工作流程和品味的方式回答这些问题。但第一次回答这些问题可能会非常困难、耗时且令人沮丧!
\n这就是为什么我花了几天时间研究做这些事情的方法,然后我将其发布为一篇名为“如何在 2022 年创建 Python 包”的博客文章博客文章。
\n那篇文章和这个答案记录了我想发布我的包时的发现扩展包时的发现
\n以下概述了您可以使用的一些工具以及可以采取的步骤,按照我在发现所有这些内容时遵循的顺序。
\n免责声明:(通常)存在其他替代工具,并且此处的大多数步骤不是强制性的。
\npip)以下概述了您可以执行的操作以及或多或少的操作方法。同样,详细的说明以及我选择某些工具、方法等的理由,可以在参考文章中找到。
\n使用诗歌进行依赖管理。
\npoetry init初始化目录中的项目或poetry new dirname为您创建新的目录结构poetry install所有依赖项poetry add packagename可以用来添加packagename为依赖项,-D如果它是开发依赖项,则使用它(即,您在开发包时需要它,但包用户不需要它。例如,black是开发依赖项的一个很好的示例)在GitHub上设置存储库来托管您的代码。
\n设置预提交挂钩以确保您的代码始终格式正确并通过 linting。这样下去.pre-commit-config.yaml。例如,下面的 YAML 检查 TOML 和 YAML 文件,确保所有文件以换行符结尾,确保所有文件的行尾标记一致,然后运行black和isort在代码上
# See https://pre-commit.com for more information\n# See https://pre-commit.com/hooks.html for more hooks\nrepos:\n\xc2\xa0 - repo: https://github.com/pre-commit/pre-commit-hooks\n\xc2\xa0 \xc2\xa0 rev: v4.0.1\n\xc2\xa0 \xc2\xa0 hooks:\n\xc2\xa0 \xc2\xa0 \xc2\xa0 - id: check-toml\n\xc2\xa0 \xc2\xa0 \xc2\xa0 - id: check-yaml\n\xc2\xa0 \xc2\xa0 \xc2\xa0 - id: end-of-file-fixer\n\xc2\xa0 \xc2\xa0 \xc2\xa0 - id: mixed-line-ending\n\xc2\xa0 - repo: https://github.com/psf/black\n\xc2\xa0 \xc2\xa0 rev: 22.3.0\n\xc2\xa0 \xc2\xa0 hooks:\n\xc2\xa0 \xc2\xa0 \xc2\xa0 - id: black\n\xc2\xa0 - repo: https://github.com/PyCQA/isort\n\xc2\xa0 \xc2\xa0 rev: 5.10.1\n\xc2\xa0 \xc2\xa0 hooks:\n\xc2\xa0 \xc2\xa0 \xc2\xa0 - id: isort\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 args: ["--profile", "black"]\nRun Code Online (Sandbox Code Playgroud)\n配置Poetry以使用测试 PyPI以确保您可以发布包并且可以下载和安装。
\npoetry config repositories.testpypi https://test.pypi.org/legacy/poetry config http-basic.testpypi __token__ pypi-your-api-token-here(__token__是一个文字,不应被替换,您的令牌在后面)。poetry build并上传您的包poetry publish -r testpypi管理您的变更日志使用Scriv
\nscriv create并编辑弹出的文件scriv collect,将所有片段收集到一个变更日志中配置要使用的 PoetryPyPI
\npoetry config pypi-token.pypi pypi-your-token-herepoetry publish --build跑一圈胜利:尝试一下pip install yourpackagename确保一切顺利;)
发布与您上传到 PyPI 的内容相匹配的 GH 版本
\n编写测试。有很多选择。pytest简单、通用且不太冗长。
\ntests/test_...test_...assert) 来检查事物(断言某些事物为 Falsy 时测试会失败);请注意,有时您甚至不需要导入pytest测试文件;例如:# In tests/test_basic_example.py\n\ndef this_test_would_definitely_fail():\n\xc2\xa0 \xc2\xa0 assert 5 > 10\n\ndef this_test_would_definitely_pass():\n\xc2\xa0 \xc2\xa0 assert 5 > 0\nRun Code Online (Sandbox Code Playgroud)\n使用命令运行测试pytest
使用tox自动化测试、检查和格式化。
\ntox.ini。您也可以将其嵌入到文件中pyproject.toml,但在撰写本文时,只有添加实际表示.ini配置的字符串才支持该功能,这很丑陋。例子tox.ini:[tox]\nisolated_build = True\nenvlist = py38,py39,py310\n\n[testenv]\ndeps =\n\xc2\xa0 \xc2\xa0 black\n\xc2\xa0 \xc2\xa0 pytest\ncommands =\n\xc2\xa0 \xc2\xa0 black --check extendedjson\n\xc2\xa0 \xc2\xa0 pytest .\nRun Code Online (Sandbox Code Playgroud)\ntox会自动理解py38to的环境来表示不同的 Python 版本(你猜是哪些版本)。标头定义了tox知道的所有环境的配置。我们安装 中列出的依赖项,然后运行 中列出的命令。py310[testenv]deps = ...commands = ...
tox对所有环境运行 tox或tox -e py39选择特定环境
使用coverage.py添加代码覆盖率
\ncoverage run --source=yourpackage --branch -m pytest .coverage html创建一个 GitHub 操作,对提交和拉取请求运行 linting 和测试
\n.github/workflows# .github/workflows/build.yaml\nname: Your amazing CI name\n\n# Run automatically on...\non:\n\xc2\xa0 push: \xc2\xa0# pushes...\n\xc2\xa0 \xc2\xa0 branches: [ main ] \xc2\xa0# to the main branch... and\n\xc2\xa0 pull_request: \xc2\xa0# on pull requests...\n\xc2\xa0 \xc2\xa0 branches: [ main ] \xc2\xa0# against the main branch.\n\n# What jobs does this workflow run?\njobs:\n\xc2\xa0 build: \xc2\xa0# There\'s a job called \xe2\x80\x9cbuild\xe2\x80\x9d which\n\xc2\xa0 \xc2\xa0 runs-on: ubuntu-latest \xc2\xa0# runs on an Ubuntu machine\n\xc2\xa0 \xc2\xa0 strategy:\n\xc2\xa0 \xc2\xa0 \xc2\xa0 matrix: \xc2\xa0# that goes through\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 python-version: ["3.8", "3.9", "3.10"] \xc2\xa0# these Python versions.\n\n\xc2\xa0 \xc2\xa0 steps: \xc2\xa0# The job \xe2\x80\x9cbuild\xe2\x80\x9d has multiple steps:\n\xc2\xa0 \xc2\xa0 \xc2\xa0 - name: Checkout sources\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 uses: actions/checkout@v2 \xc2\xa0# Checkout the repository into the runner,\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 - name: Setup Python\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 uses: actions/setup-python@v2 \xc2\xa0# then set up Python,\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 with:\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 python-version: ${{ matrix.python-version }} \xc2\xa0# with the version that is currently \xe2\x80\x9cselected\xe2\x80\x9d...\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 - name: Install dependencies\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 run: | \xc2\xa0# Then run these commands\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 python -m pip install --upgrade pip\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 python -m pip install tox tox-gh-actions \xc2\xa0# install two dependencies...\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 - name: Run tox\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 run: tox \xc2\xa0# and finally run tox.\nRun Code Online (Sandbox Code Playgroud)\n请注意,上面我们安装了 tox和一个名为 的插件tox-gh-actions。\n此插件将使 tox 知道 GH 操作运行程序中设置的 Python 版本,这将允许我们指定 tox 在这种情况下将运行的环境。\我们只需要在文件中设置一个对应关系tox.ini:
# tox.ini\n# ...\n[gh-actions]\npython =\n\xc2\xa0 \xc2\xa0 3.8: py38\n\xc2\xa0 \xc2\xa0 3.9: py39\n\xc2\xa0 \xc2\xa0 3.10: py310\nRun Code Online (Sandbox Code Playgroud)\nxml(这是 Codecov 理解的格式)# ...\n\xc2\xa0 \xc2\xa0 \xc2\xa0 - name: Upload coverage to Codecov\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 uses: codecov/codecov-action@v2\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 with:\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 fail_ci_if_error: true\nRun Code Online (Sandbox Code Playgroud)\nname: Publish to PyPI\n\non:\n\xc2\xa0 release:\n\xc2\xa0 \xc2\xa0 types: [ published ]\n\xc2\xa0 \xc2\xa0 branches: [ main ]\n\xc2\xa0 workflow_dispatch:\n\njobs:\n\xc2\xa0 build-and-publish:\n\xc2\xa0 \xc2\xa0 runs-on: ubuntu-latest\n\n\xc2\xa0 \xc2\xa0 steps:\n\xc2\xa0 \xc2\xa0 \xc2\xa0 # Checkout and set up Python\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 - name: Install poetry and dependencies\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 run: |\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 python -m pip install --upgrade pip\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 python -m pip install poetry\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 - name: Configure poetry\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 env:\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 pypi_token: ${{ secrets.PyPI_TOKEN }} \xc2\xa0# You set this manually as a secret in your repository\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 run: poetry config pypi-token.pypi $pypi_token\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 - name: Build and publish\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 run: poetry publish --build\nRun Code Online (Sandbox Code Playgroud)\n稍微整理一下
\n| 归档时间: |
|
| 查看次数: |
836 次 |
| 最近记录: |