[Distutils] Detecting if a package was installed with an "extra"

Phillip J. Eby pje at telecommunity.com
Tue Mar 20 16:59:04 CET 2007


At 11:10 AM 3/20/2007 -0400, Nathan R. Yergler wrote:
>I'm working on an improvement to my RDFa parser and because it will
>rely on an external tool, I'm using an extra to require it to be
>explicitly required.  Is there a way to tell at runtime if an egg (for
>the code running) was installed with an extra.  For example, detecting
>the difference between:
>
>easy_install foo[bar]
>
>and
>
>easy_install foo
>
>when in foo.py?

First answer that comes to mind:

     try:
         pkg_resources.require('foo[bar]')
     except pkg_resources.ResolutionError:
         # it wasn't installed, or there's a version conflict
     else:
         # good to go

There might be other ways, but this is what I'd do.

Note that for entry points (including scripts), the entry point can depend 
on extras, and this means that trying to load (or .require()) the entry 
point will raise similar exceptions if the extra isn't found.



More information about the Distutils-SIG mailing list