[Distutils] Getting an egg's version: better way?
Nathan R. Yergler
nathan at yergler.net
Fri Mar 16 14:58:57 CET 2007
Thanks guys, that helps a lot.
On 3/15/07, Phillip J. Eby <pje at telecommunity.com> wrote:
> At 10:48 AM 3/15/2007 -0400, Nathan R. Yergler wrote:
> >I have a situation where I want to get the version of an egg that's
> >installed. I figured out the following, but wanted to know if I'm
> >missing something obvious that's easier::
> >
> > >>> import pkg_resources
> > >>> p = pkg_resources.get_provider("cctagutils")
> > >>> for line in p.get_metadata_lines("PKG-INFO"):
> > ... if line.find('Version: ') == 0:
> > ... version = line.split()[-1]
>
> The problem with using get_provider() and a module name, is that it cannot
> conclusively associate a package with a specific project. Thus, your code
> above has a bug: it will not find a version of a package that's installed
> by a system packaging tool such as RPM, or a bdist .exe or .msi on Windows.
>
> You need to use a project name instead of a package/module name, e.g.:
>
> version = pkg_resources.require('ProjectNameHere')[0].version
>
> This will work even with projects installed using system packaging tools,
> as it explicitly indicates what project's PKG-INFO will be read, if in fact
> it needs to be read at all. (In most cases, pkg_resources will get the
> project's version number from a filename, without needing to open any files.)
>
>
More information about the Distutils-SIG
mailing list