Dimitri Tcaciuc Dimitri Tcaciuc - 2 months ago 18
Python Question

Standard way to embed version into python package?

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
already. Alternative solution that I found was to have
import __version__
in my
and then have
generated by


Not directly an answer to your question, but you should consider naming it __version__, not version.

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.

Edit: as mentioned by S.Lott (Thank you!), PEP 8 says it explicitly:

Version Bookkeeping

If you have to have Subversion, CVS, or RCS crud in your source file, do it as follows.

    __version__ = "$Revision: 63990 $"
    # $Source$

These lines should be included after the module's docstring, before any other code, separated by a blank line above and below.

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).