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

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

    We wanted to put the meta information about our package pypackagery in __init__.py, but could not since it has third-party dependencies as PJ Eby already pointed out (see his answer and the warning regarding the race condition).

    We solved it by creating a separate module pypackagery_meta.py that contains only the meta information:

    """Define meta information about pypackagery package."""
    
    __title__ = 'pypackagery'
    __description__ = ('Package a subset of a monorepo and '
                       'determine the dependent packages.')
    __url__ = 'https://github.com/Parquery/pypackagery'
    __version__ = '1.0.0'
    __author__ = 'Marko Ristin'
    __author_email__ = 'marko.ristin@gmail.com'
    __license__ = 'MIT'
    __copyright__ = 'Copyright 2018 Parquery AG'
    

    then imported the meta information in packagery/__init__.py:

    # ...
    
    from pypackagery_meta import __title__, __description__, __url__, \
        __version__, __author__, __author_email__, \
        __license__, __copyright__
    
    # ...
    

    and finally used it in setup.py:

    import pypackagery_meta
    
    setup(
        name=pypackagery_meta.__title__,
        version=pypackagery_meta.__version__,
        description=pypackagery_meta.__description__,
        long_description=long_description,
        url=pypackagery_meta.__url__,
        author=pypackagery_meta.__author__,
        author_email=pypackagery_meta.__author_email__,
        # ...
        py_modules=['packagery', 'pypackagery_meta'],
     )
    

    You must include pypackagery_meta into your package with py_modules setup argument. Otherwise, you can not import it upon installation since the packaged distribution would lack it.

提交回复
热议问题