简体   繁体   English

setup.cfg 元数据中的“version”条目被忽略

[英]The "version" entry in the metadata of setup.cfg is being ignored

I wanted to write a new Python package that I want to make available via PyPI.我想编写一个新的 Python 包,我想通过 PyPI 提供它。 In the past I always used setup.py .过去我总是使用setup.py But this time I decided to embrace new best practices of using setup.cfg instead.但这次我决定采用使用setup.cfg新最佳实践。 So I started reading a little bit of the documentation, mainly https://setuptools.pypa.io/en/latest/userguide/declarative_config.html .所以我开始阅读一些文档,主要是https://setuptools.pypa.io/en/latest/userguide/declarative_config.html

I also decided to use pyscaffold for the generation of the files.我还决定使用pyscaffold来生成文件。 pyscaffold generated a setup.cfg file for me and I added (just for testing purposes) version = 5.1.1 in under the metadata section, as described in the documentation above. pyscaffold为我生成了一个setup.cfg文件,我在metadata部分下添加了(仅用于测试目的) version = 5.1.1 ,如上面的文档中所述。

For convenience I'm using anaconda and created a new empty environment:为方便起见,我使用 anaconda 并创建了一个新的空环境:

$ python --version
Python 3.9.7
$  pip list
...
PyScaffold                    4.1.1
setuptools                    58.4.0
setuptools-scm                6.3.2
wheel                         0.37.0

But when I executed pip install -e .但是当我执行pip install -e . , the version was ignored and pip assigned a different one: ,该版本被忽略并且pip分配了一个不同的版本:

$ pip install -e .
Obtaining file:///tmp/test_package
  Installing build dependencies ... done
  Checking if build backend supports build_editable ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Installing collected packages: test-package
  Attempting uninstall: test-package
    Found existing installation: test-package 0.0.post1.dev10+g3ed39c8.d20211106
    Uninstalling test-package-0.0.post1.dev10+g3ed39c8.d20211106:
      Successfully uninstalled test-package-0.0.post1.dev10+g3ed39c8.d20211106
  Running setup.py develop for test-package
Successfully installed test-package-0.0.post1.dev1+g228b46c

https://stackoverflow.com/a/27077610/1480131 mentions that setuptools version 30.3.0 supports putting metadata in setup.cfg , I'm using 58.4.0 , so a much more recent version. https://stackoverflow.com/a/27077610/1480131提到 setuptools 版本 30.3.0 支持将元数据放入setup.cfg ,我使用的是58.4.0 ,所以是更新的版本。

I also tested with different formats like version = file:VERSION.txt but that didn't help either, pip simply ignores the version entry.我还测试了不同的格式,如version = file:VERSION.txt但这也无济于事, pip只是忽略了version条目。

What am I missing?我错过了什么? What am I doing wrong?我究竟做错了什么?

I prepared a small git repo with the files in order to be able to reproduce the error: https://github.com/shaoran/test_package Does anybody get a different result?我准备了一个带有文件的小型 git repo 以便能够重现错误: https : //github.com/shaoran/test_package有人得到不同的结果吗?

That's because pyscaffold generated a project that uses setuptools-scm for version detection.那是因为pyscaffold生成了一个使用setuptools-scm进行版本检测的项目。 When setuptools-scm is used, the version is not read from version metadata, but parsed from the repository tags.使用setuptools-scm ,版本不是从version元数据中读取的,而是从存储库标签中解析出来的。 The version 0.0.post1.dev10+g3ed39c8.d20211106 can be read as follows: 0.0.post1.dev10+g3ed39c8.d20211106版本可以读取如下:

  • 0.0.post1 - dummy version since you don't have any tags in repo yet; 0.0.post1 - 虚拟版本,因为您在 repo 中还没有任何标签;
  • dev10 - you have ten commits that are not included in any version tag (basically the amount of commits you made since tagging last); dev10 - 您有 10 个未包含在任何版本标记中的提交(基本上是自上次标记以来您所做的提交数量);
  • g3ed39c8 - the short hash of commit you have installed from is 3ed39c8 (prefix g means it is a Git repo); g3ed39c8 - 您安装的提交的短哈希是3ed39c8 (前缀g表示它是一个 Git 存储库);
  • d20211106 - d means you have installed from a dirty state (some files versioned by Git were modified), the rest is just the timestamp. d20211106 - d表示您已从脏状态安装(某些由 Git 版本控制的文件被修改),其余只是时间戳。

If you want to use the version metadata from setup.cfg instead, just drop setuptools-scm activation in setup.py :如果您想使用setup.cfg的版本元数据,只需在setup.py删除setuptools-scm激活:

if __name__ == "__main__":
    try:
        setup()
    except:
        ...

You can then also clean up pyproject.toml , although this isn't required explicitly:然后您还可以清理pyproject.toml ,尽管这不是明确要求的:

[build-system]
requires = ["setuptools>=46.1.0", "wheel"]
build-backend = "setuptools.build_meta"

If you want to keep using setuptools-scm (which IMO is a great tool to prevent you from accidentally releasing dists with same version, but different contents), then just add a new tag to start versioning from:如果您想继续使用setuptools-scm (IMO 是一个很好的工具,可以防止您意外发布具有相同版本但内容不同的setuptools-scm ),那么只需添加一个新标签即可从以下位置开始版本控制:

$ git tag -a 5.1.1 -m 'start versioning'

If you had a clean repo state (no modified files), pip install -e .如果你有一个干净的 repo 状态(没有修改过的文件), pip install -e . will install the version 5.1.1 right away.将立即安装 5.1.1 版。 Otherwise, you will get a 5.1.1 with a suffix.否则,您将获得带有后缀的 5.1.1。

The neat part of using setuptools-scm is that the version metadata in setup.cfg is ignored, so you don't have to bump it yourself and can safely remove version = 5.1.1 line.用整齐的一部分setuptools-scm是在版本的元数据setup.cfg被忽略,所以你不必自己碰到它,可以安全地删除version = 5.1.1行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM