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

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

    Cleaning up https://stackoverflow.com/a/12413800 from @gringo-suave:

    from itertools import ifilter
    from os import path
    from ast import parse
    
    with open(path.join('package_name', '__init__.py')) as f:
        __version__ = parse(next(ifilter(lambda line: line.startswith('__version__'),
                                         f))).body[0].value.s
    

提交回复
热议问题