How can I get the version defined in setup.py (setuptools) in my package?

前端 未结 16 789
无人及你
无人及你 2020-11-27 09:47

How could I get the version defined in setup.py from my package (for --version, or other purposes)?

16条回答
  •  温柔的废话
    2020-11-27 10:35

    This should also work, using regular expressions and depending on the metadata fields to have a format like this:

    __fieldname__ = 'value'
    

    Use the following at the beginning of your setup.py:

    import re
    main_py = open('yourmodule.py').read()
    metadata = dict(re.findall("__([a-z]+)__ = '([^']+)'", main_py))
    

    After that, you can use the metadata in your script like this:

    print 'Author is:', metadata['author']
    print 'Version is:', metadata['version']
    

提交回复
热议问题