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

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

    The best technique is to define __version__ in your product code, then import it into setup.py from there. This gives you a value you can read in your running module, and have only one place to define it.

    The values in setup.py are not installed, and setup.py doesn't stick around after installation.

    What I did (for example) in coverage.py:

    # coverage/__init__.py
    __version__ = "3.2"
    
    
    # setup.py
    from coverage import __version__
    
    setup(
        name = 'coverage',
        version = __version__,
        ...
        )
    

    UPDATE (2017): coverage.py no longer imports itself to get the version. Importing your own code can make it uninstallable, because you product code will try to import dependencies, which aren't installed yet, because setup.py is what installs them.

提交回复
热议问题