Detecting if a package was installed with an "extra"

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Detecting if a package was installed with an "extra"

Nathan R. Yergler-4
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?

Thanks,

Nathan
_______________________________________________
Distutils-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/distutils-sig
Reply | Threaded
Open this post in threaded view
|

Re: Detecting if a package was installed with an "extra"

PJ Eby
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.

_______________________________________________
Distutils-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/distutils-sig