Is there a standard way to associate version string with a Python package in such way that I could do the following?
import foo print(foo.version)
I would imagine there’s some way to retrieve that data without any extra hardcoding, since minor/major strings are specified in setup.py already. Alternative solution that I found was to have import __version__ in my foo/__init__.py and then have __version__.py generated by setup.py.
Not directly an answer to your question, but you should consider naming it
__version__, notversion.This is almost a quasi-standard. Many modules in the standard library use
__version__, and this is also used in lots of 3rd-party modules, so it’s the quasi-standard.Usually,
__version__is a string, but sometimes it’s also a float or tuple.As mentioned by S.Lott (Thank you!), PEP 8 says it explicitly:
You should also make sure that the version number conforms to the format described in PEP 440 (PEP 386 a previous version of this standard).