0

I have a packaged project mytools which uses setuptools' setup to store its version in a setup.py project file e.g.

import setuptools

setuptools.setup(
name='mytools',
version='0.1.0'
)

I'd like to get the common mytools.__version__ feature based on the version value e.g.

import mytools
mytools.__version__
>>>'0.1.0'

Is there native / simple way in setuptools to do so? Couldn't find a reference to __version__ in setuptools. Furthermore, I don't want to store the version in __init__.py because I'd prefer to keep the version in its current place (setup.py). The many answers to similar questions do not speak to my specific problem, e.g. How can I get the version defined in setup.py (setuptools) in my package?

ivegotaquestion
  • 573
  • 1
  • 7
  • 19
  • 1
    `__version__ = importlib.metadata.version('mytools')`? It is just another variable that you have to define in your code, no magic constants are supplied by setuptools. – hoefling Jun 15 '20 at 09:39

1 Answers1

1

Adding __version__ to all top-level modules and packages is a recommendation from PEP 396.

Lately I have seen growing concerns raised about this recommendation and its actual usefulness, for example here:

With that said...

Such a thing is often solved like the following:

# my_top_level_module/__init__.py

import importlib.metadata

__version__ = importlib.metadata.version('MyProject')

References:

sinoroc
  • 18,409
  • 2
  • 39
  • 70