What is the correct way to share package version with setup.py and the package?

后端 未结 7 1768
盖世英雄少女心
盖世英雄少女心 2020-12-07 08:42

With distutils, setuptools, etc. a package version is specified in setup.py:

# file: setup.py
...
setup(
name=\'foobar         


        
7条回答
  •  自闭症患者
    2020-12-07 09:06

    Based on the accepted answer and comments, this is what I ended up doing:

    file: setup.py

    setup(
        name='foobar',
        version='1.0.0',
        # other attributes
    )
    

    file: __init__.py

    from pkg_resources import get_distribution, DistributionNotFound
    
    __project__ = 'foobar'
    __version__ = None  # required for initial installation
    
    try:
        __version__ = get_distribution(__project__).version
    except DistributionNotFound:
        VERSION = __project__ + '-' + '(local)'
    else:
        VERSION = __project__ + '-' + __version__
        from foobar import foo
        from foobar.bar import Bar
    

    Explanation:

    • __project__ is the name of the project to install which may be different than the name of the package

    • VERSION is what I display in my command-line interfaces when --version is requested

    • the additional imports (for the simplified package interface) only occur if the project has actually been installed

提交回复
热议问题