我如何才能找出在 setup.py 中传递给安装程序的版本?

How can I find out what version is passed to setup in setup.py?

我要找的是这样的东西:

python setup.py show-version

这样我就可以为发布创建一个 git 标记,作为构建和发布结构作业的一部分。

因为那不存在,所以我得到了:

    import setuptools
    original = sys.modules['setuptools']

    class Fake(object):
        def __call__(self, *args, **kw):
            self.args, self.kw = args, kw

    fake_setuptools = ModuleType('fake_setuptools')
    for name in dir(setuptools):
        setattr(fake_setuptools, name, Fake())

    sys.modules['setuptools'] = fake_setuptools

    with open('setup.py') as source:
        exec(source.read())

    print(fake_setuptools.setup.kw['version'])

坦率地说,这太可怕了!

我应该怎么做?

Setuptools 为安装脚本调用提供了一个 --version 标志。

$ python setup.py --version

或者,如果您需要包中的其他元数据,jaraco.packaging 提供了一个 show 命令,可用于显示包中的任意数量的属性。

可以从 setup.py 获得一大堆信息,包括我正在寻找的信息:

$ python setup.py --help
...
Information display options (just display information, ignore any commands)
  --help-commands     list all available commands
  --name              print package name
  --version (-V)      print package version
  --fullname          print <package name>-<version>
  --author            print the author's name
  --author-email      print the author's email address
  --maintainer        print the maintainer's name
  --maintainer-email  print the maintainer's email address
  --contact           print the maintainer's name if known, else the author's
  --contact-email     print the maintainer's email address if known, else the
                      author's
  --url               print the URL for this package
  --license           print the license of the package
  --licence           alias for --license
  --description       print the package description
  --long-description  print the long package description
  --platforms         print the list of platforms
  --classifiers       print the list of classifiers
  --keywords          print the list of keywords
  --provides          print the list of packages/modules provided
  --requires          print the list of packages/modules required
  --obsoletes         print the list of packages/modules made obsolete