简体   繁体   中英

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. This way I can make sure that the results obtained are from this particular version of my package.

The following is the contents of my setup.py file (simplified to have the necessary part)

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:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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