简体   繁体   English

从 distutils setup.py 获取版本

[英]Get the version from distutils setup.py

How can I import or read the VERSION from the setup.py file so that I can log the version at runtime.如何从 setup.py 文件中导入或读取 VERSION,以便在运行时记录版本。 This way I can make sure that the results obtained are from this particular version of my package.这样我可以确保获得的结果来自我的 package 的这个特定版本。

The following is the contents of my setup.py file (simplified to have the necessary part)以下是我的 setup.py 文件的内容(简化为有必要的部分)

import distutils.core
VERSION = '0.1.0'
LICENSE = 'GPLv2'
distutils.core.setup(**KWARGS)

When I try to do: import setup I get the following error:当我尝试这样做时: import setup 我收到以下错误:

distutils.core.setup(**KWARGS)
usr/lib/python2.6/distutils/core.pyc in setup(**attrs)
        ok = dist.parse_command_line()
    except DistutilsArgError, msg:
        raise SystemExit, gen_usage(dist.script_name) + "\nerror: %s" % msg

    if DEBUG:

SystemExit: 

error: no commands supplied

In yor example, setup is excecuted automatically, you have to replace:在您的示例中,安装程序会自动执行,您必须替换:

distutils.core.setup(**KWARGS)

with:和:

if __name__ == '__main__':
    distutils.core.setup(**KWARGS)

Like this, setup is only executed if you actually run the setup.py像这样,只有在你实际运行 setup.py 时才会执行 setup

There is a way to get the version from your setup script:有一种方法可以从您的安装脚本中获取版本:

python setup.py --version

But I'm not sure I understand what you mean with “log the version at runtime”;但是我不确定我是否理解“在运行时记录版本”的意思; the setup script is normally not installed with your modules, so people use other ways to put a version number in their code, like a __version__ attribute in their module or __init__.py file.安装脚本通常不会与您的模块一起安装,因此人们使用其他方式将版本号放入他们的代码中,例如模块中的__version__属性或__init__.py文件。

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

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