Installing modules via setuptools in a script

Robert Kern robert.kern at gmail.com
Sat Nov 24 17:33:37 EST 2007


Thorsten Kampe wrote:

> Hi,
> 
> 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:

  # http://peak.telecommunity.com/DevCenter/setuptools#basic-use
  setup(name="foo",
        ...
        install_requires = [
          'some_package >= 1.0',
          'another_package',
        ],
  )

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

But, please be sure that that your needs are special.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco




More information about the Python-list mailing list