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

前端 未结 16 815
无人及你
无人及你 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:16

    There's a thousand ways to skin a cat -- here's mine:

    # Copied from (and hacked):
    # https://github.com/pypa/virtualenv/blob/develop/setup.py#L42
    def get_version(filename):
        import os
        import re
    
        here = os.path.dirname(os.path.abspath(__file__))
        f = open(os.path.join(here, filename))
        version_file = f.read()
        f.close()
        version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]",
                                  version_file, re.M)
        if version_match:
            return version_match.group(1)
        raise RuntimeError("Unable to find version string.")
    

提交回复
热议问题