Installing modules via setuptools in a script

Thorsten Kampe thorsten at thorstenkampe.de
Sun Nov 25 14:30:28 CET 2007


* Robert Kern (Sat, 24 Nov 2007 16:33:37 -0600)
> Thorsten Kampe wrote:
> > can anyone give me a short code snippet how to install a missing 
> > module via setuptools (assuming setuptools is already installed)?!
> > 
> > Something like this:
> > 
> > try:
> >     import missing_module
> > except import_error
> >     import setuptools
> >     setuptools.whatever.install(missing_module)
> 
> The recommended way to handle dependencies using setuptools is to specify them
> in the install_requires metadata in the setup() function call in your setup.py:

It's just a simple script - no package. So I don't even have a 
setup.py.

> However, if you have special needs that really do require downloading the
> dependency at runtime instead of install-time:
> 
>   #
> http://peak.telecommunity.com/DevCenter/PkgResources#workingset-methods-and-attributes
> 
>   import pkg_resources
>   pkg_resources.resolve('some_package >= 1.0')
>   pkg_resources.resolve('another_package')
> 
>   import some_package
>   import another_package

[5]>>> pkg_resources.working_set.resolve('betterprint')
----------------------------------------------------------------------
-----
AttributeError                            Traceback (most recent call 
last)

F:\program files\python\<ipython console> in <module>()

F:\program files\python\lib\site-packages\setuptools-0.6c5-py2.5.egg
\pkg_resou
rces.py in resolve(self=<pkg_resources.WorkingSet object at 
0x01457710>, requi
rements=['n', 'i', 'r', 'p', 'r', 'e', 't', 't', 'e', 'b'], env=None, 
installe
r=None)
    472                 # Ignore cyclic or redundant dependencies
    473                 continue
--> 474             dist = best.get(req.key)
        dist = undefined
        best.get = <built-in method get of dict object at 0x016BC660>
        req.key = undefined
    475             if dist is None:
    476                 # Find the best distribution and add it to the 
map

AttributeError: 'str' object has no attribute 'key'



More information about the Python-list mailing list