Or my preferred way: $cat foobar/__init__.py __version__ = "0.9.33"
and import foobar should not trigger code execution anyway IMHO so $ python -c 'import foobar; print foobar.__version__' 0.9.33
That doesn't work in all cases.
No it is not that the case and I'm going to bore you probably (sorry). The "source" dir where the module foobar is located: ~someuser/foobar-1.0/setup.py ~someuser/foobar-1.0/foobar/__init__.py $> cat ~someuser/foobar-1.0/foobar/__init__.py __version__ = "1.0" Assume the previous module is installed already under: /usr/lib/python2.5/site-lib/foobar/ /usr/lib/python2.5/site-lib/foobar/__init__.py $> cat /usr/lib/python2.5/site-lib/foobar/__init__.py __version__ = "0.9" (please note the site-lib subidr, where all the module not belonging to the python standard library are located). $~someuser/foobar-1.0/> python -c 'import foobar; print foobar.__version__' This return (or it does on my python interpreter): "1.0" not "0.9" The case is different for the standard libraries, in fact: $~someuser> touch os.py $~someuser> python -c 'import os; print os.__file__' this will return: /usr/lib/python2.5/os.py(c) There's no need to have foobar "installed" to reflect the correct __version__. Then we need to agree on what do we mean for install and packaging.... "python setup.py install", "python setup.py bdist_rpm/wininst" or during a complete deb/rpm package build? In the latter the version the "version" is the one available in the spec/deb files and it cannot be reflected from the sources anyway, no matter how hard one try to do it.
Your example is of an external query of the version of an installed module. You also need to query the version -before- it is installed (during the packaging phase) on sys.path, and also from -within- the foobar module itself. Your code does not handle those cases. An attempt to 'import foobar; print foobar.__version__' from a setup.py inside foobar won't find foobar.
Finding modules can always be forced using PYTHONPATH (like in case of foobar-1.0/src/foobar layout) or from setup.py inserting into sys.path the subdir: that's the easiest and in my opinion the best way to do it during build/install stages. Regards, Antonio