python项目构建_使用Python项目模板构建和自动化Python项目

东郭勇
2023-12-01

python项目构建

To create a project that other people can use and contribute to, you need to follow a specific directory structure. Moreover, releasing a new version should be as simple and painless as possible. For my projects, I use a template that has the structure already in place, and comes with automation for almost every part of a release.

要创建其他人可以使用和参与的项目,您需要遵循特定的目录结构。 此外,发布新版本应该尽可能简单和轻松。 对于我的项目,我使用的模板具有已经存在的结构,并且几乎针对发行版的每个部分都具有自动化功能。

The Python Project Template is my approach to this. It comes with a good (IMO) structure and automation.

我的方法是使用Python项目模板 。 它具有良好的(IMO)结构和自动化。

What does it include? A whole lot of things. Documentation, Sphinx configuration, a simple test suite, a setup.py file, some AUR stuff, and perhaps the most important part — the release script. It can automate a lot of tasks that are part of a release.

它包括什么? 很多事情。 文档,Sphinx配置,简单的测试套件, setup.py文件,一些AUR内容,也许是最重要的部分- 发布脚本。 它可以自动化发布中的许多任务。

You see, releasing a package is error-prone. There are a lot of things that can go wrong:

您会看到,发布软件包很容易出错。 有很多事情可能出错:

  • Version numbers. They may appear in code comments, Sphinx configuration, README files and documentation, and setup.py. Some people claim to have “solutions” for this. Most of those solutions don’t work right — either they import a file from the project (which may break if __init__.py is too magical), read a file from that place (which might not get included properly), or use some setuptools extension to get the version from VCS or whatever (which needs to be installed before the package). Using sed to fix the version numbers is much simpler.
  • Forgetting about changelogs.
  • Not updating translations or other important files.
  • 版本号。 它们可能出现在代码注释,Sphinx配置,自述文件和文档以及setup.py中。 有些人声称对此有“解决方案”。 这些解决方案中的大多数都无法正常工作-他们从项目中导入文件(如果__init__.py太神奇了 ,可能会中断),从该位置读取文件(可能未正确包含)或使用一些设置工具扩展以从VCS或任何其他版本(需要在软件包之前安装)中获取版本。 使用sed修复版本号要简单得多。
  • 忘记变更日志。
  • 不更新翻译或其他重要文件。

We’ve had quite a few botched releases in the Nikola project. I wrote a checklist to prevent things like those. You may notice that the most prominent step is to run a release script. This step replaced 21 others — now the checklist only talks about writing announcements, sending e-mails, updating the website, and doing some GitHub stuff that is not yet automated.

Nikola项目中,我们已经发布了许多拙劣的版本。 我写了一份清单来防止类似的事情。 您可能会注意到,最重要的步骤是运行发行脚本。 此步骤替代了其他21个步骤-现在清单仅讨论撰写公告,发送电子邮件,更新网站以及执行一些尚未实现自动化的GitHub工作。

The template promotes a release early, release often workflow: since making a new release requires almost no human intervention, you might as well do it every time you make a bunch of changes. In my projects, everything gets automated, and it might as well be possible in yours.

该模板可以提早发布版本,经常发布版本的工作流程:由于制作新版本几乎不需要人工干预,因此每次进行大量更改时也可以这样做。 在我的项目中,一切都变得自动化了,而且在您的项目中也有可能实现。

完整的功能列表(自v2.1.5起) (The complete feature list (as of v2.1.5))

  • pre-configured Sphinx with:
    • CONTRIBUTING.rst guide (used by GitHub when sending a pull request or an issue)
    • LICENSE.rst
    • an empty CHANGELOG.rst
    • README.rst
    • a bare-bones index.rst page
  • The exact same files in /, which are fragile and MAY NOT be modified as they are replaced with copies in /docs by the release script
  • __init__.py, __main__.py and template.py files in the Python package directory
  • A setup.py file that could be good enough for people, and that supports entry_points (see https://go.chriswarrick.com/entry_points).
  • tests/ containing some Is My Python Sane?-style tests (using pytest)
  • An automated global update script (.pypt/PYPT-UPDATE)
  • Entry points configuration ready to be uncommented (and a matching __main__.py file)
  • Add-ons for Qt users (in pypt-extras/Qt)
  • A sample hook for AUR updates (in pypt-extras/AUR)
  • PKGBUILDs for the Arch Linux User Repository (AUR)
  • A state-of-the-art release script, the operations of which are:
    • querying the user for version number, commit message and changes
    • updating /docs/CHANGELOG.rst
    • bumping the version number in all the files, changing dates where necessary
    • copying over /docs/README.rst, /docs/CHANGELOG.rst and /docs/CONTRIBUTING.rst to /
    • locale generation (via the .pypt/localegen script)
    • running import $PROJECTLC and the test suite
    • uploading a source distribution and a wheel to PyPI
    • Making a Git commit and tagging the release
    • creating a GitHub Releases entry
    • updating the AUR packages (by using hooks)
  • 使用以下功能预配置Sphinx:
    • CONTRIBUTING.rst指南(在发送拉取请求或问题时由GitHub使用)
    • 许可
    • 空的CHANGELOG.rst
    • 自述文件
    • 准系统index.rst页面
  • /中完全相同的文件,这些文件非常脆弱,并且可能无法修改,因为发行脚本将它们替换为/ docs中的副本
  • Python软件包目录中的__init__ . py__ main__.pytemplate.py文件
  • 一个setup.py文件,可能足以满足人们的需求,并且支持entry_points (请参阅https://go.chriswarrick.com/entry_points )。
  • tests /包含一些Is My Python Sane?风格的测试(使用pytest
  • 自动全局更新脚本( .pypt / PYPT-UPDATE
  • 入口点配置已准备好取消注释(以及匹配的__main__.py文件)
  • Qt用户的附加组件(在pypt-extras / Qt中
  • AUR更新的示例挂钩(在pypt-extras / AUR中
  • 适用于Arch Linux用户存储库(AUR)的PKGBUILD
  • 最新的发行脚本,其操作是:
    • 向用户查询版本号,提交消息和更改
    • 更新/docs/CHANGELOG.rst
    • 在所有文件中增加版本号,并在必要时更改日期
    • 复制过/docs/README.rst,/docs/CHANGELOG.rst/docs/CONTRIBUTING.rst/
    • 语言环境生成(通过.pypt / localegen脚本)
    • 运行导入$ PROJECTLC和测试套件
    • 上载源代码分布和转盘到PyPI
    • 进行Git提交并标记发布
    • 创建一个GitHub Releases条目
    • 更新AUR软件包(通过使用挂钩)

System Message: ERROR/3 (<string>, line 76)

系统消息:ERROR / 3( <string> ,第76行)

Content block expected for the “raw” directive; none found.

“原始”指令的预期内容块; 没有找到。


.. raw:: html
    <div style="text-align: center;">
    <a href="https://github.com/Kwpolska/python-project-template" class="btn btn-lg btn-info"><i class="fa fa-github"></i> Check it out on GitHub</a>
    </div>


翻译自: https://www.pybloggers.com/2017/04/structuring-and-automating-a-python-project-with-the-python-project-template/

python项目构建

 类似资料: