[Distutils] automating __version__' ing issues

Phillip J. Eby pje at telecommunity.com
Tue May 15 19:59:57 CEST 2007


At 01:58 PM 5/14/2007 +0100, Alexander Schmolck wrote:
>"Phillip J. Eby" <pje at telecommunity.com> writes:
> > At 01:09 AM 5/14/2007 +0100, Alexander Schmolck wrote:
> >>"Phillip J. Eby" <pje at telecommunity.com> writes:
> >>
> >> >>Here's the rub-- it must be in setup.py and you probably want
> >> >>to give your package access to it so that it can be reported. It
> >> >>appears that the current idiom for solving this dilemma is to but a
> >> >>release.py or version.py file in your package, which is sucked up into
> >> >>the setup.py file with execfile (see
> >> >><http://kid-templating.org/trac/browser/trunk/setup.py> for an
> >> >>example). You then manually maintain the version number in one place
> >> >>(the release.py file).
> >> >
> >> > Yep, this is what most people do.
> >>
> >>Is there a standard trick to get the desired svn-revision? Running
> >>`'svnversion > __svn_version__.py`` in setup.py when a distrubtion-building
> >>command is issued and then importing that in a hand-maintained
> >>``version.py``/``release.py`` file should work, but is there a way to check
> >>whether a setup.py command falls into a certain category?
> >
> > I don't understand what you're trying to do.
>
>I'd like to have a mechanism that has me specify two things in *one* central
>location:
>
>1. A self-chosen version number, e.g. "1.1"

Put this in setup.py.


>2. A release flag (e.g. official_release=False, meaning it's an official and
>    not a development release)

Put this in setup.cfg for a development release (or leave blank for official):

    [egg_info]
    tag_svn_revision = 1
    tag_build = dev


>Now in all other locations I'd like to get a version string that either is
>"1.1" if release is True "1.1.dev43" (or whatever the svn revision) if release
>is False.
>
>All other locations means at least setup.py and a __init__.py (via import or
>execfile if needs be).

At runtime, you can use this code to get the version of your project 
(or any other project):

    from pkg_resources import require
    __version__ = require('MyProjectname')[0].version

Where 'MyProjectname' is your setup(name=...) name.  (I.e., the name 
of your distribution.)

You can also do this at the interpreter prompt, e.g.:

Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> from pkg_resources import require
 >>> require('PEAK')
[peak 0.5a4.dev-r2287 
(c:\python23\lib\site-packages\peak-0.5a4.dev_r2287-py2.3-win32.egg), 
DecoratorTools 1.4 (c:\cygwin\home\pje\projects\decoratortools), 
zconfig 2.3 (c:\python23\lib\site-packages\zconfig-2.3-py2.3.egg), 
wsgiref 0.1.2 (c:\cygwin\home\pje\projects\wsgiref), SymbolType 1.0 
(c:\cygwin\home\pje\projects\symboltype), Importing 1.9.3 
(c:\cygwin\home\pje\projects\importing), PyProtocols 1.0a0dev-r2302 
(c:\cygwin\home\pje\projects\pyprotocols\src), ruledispatch 
0.5a0.dev-r2287 
(c:\python23\lib\site-packages\ruledispatch-0.5a0.dev_r2287-py2.3-win32.egg)]
 >>>

As you can see, require() returns a list of objects representing all 
the packages needed to meet the given request, and the default repr() 
inlcudes both version numbers and file paths.  This is really the 
best way to find out what version(s) of something somebody is using, 
as it includes your dependencies as well as the base package.


>p.s. I notice that a cc: or to: to your email address bounces -- is that
>intended?

No, and AFAIK it's not happening to anybody else.



More information about the Distutils-SIG mailing list