Installing modules via setuptools in a script

Robert Kern robert.kern at gmail.com
Mon Nov 26 11:25:35 EST 2007


Thorsten Kampe wrote:
> * Robert Kern (Mon, 26 Nov 2007 04:34:17 -0600)
>> Thorsten Kampe wrote:
>>> * 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.
> [...]
>> My apologies for misleading you. There is no easy way to do this. Here is a
>> roundabout way which might be suitable for a throwaway hack script. If it's not
>> a throwaway hack script, then please heed Ben's advice. Alternatively, just
>> distribute betterprint along with your script and save yourself the headache.
>>
>>
>> In [1]: import betterprint
>> ---------------------------------------------------------------------------
>> ImportError                               Traceback (most recent call last)
>>
>> /Users/rkern/<ipython console> in <module>()
>>
>> ImportError: No module named betterprint
>>
>> In [2]: import pkg_resources
>>
>> In [3]: from setuptools.dist import Distribution
>>
>> In [4]:
>> pkg_resources.working_set.resolve(pkg_resources.parse_requirements('betterprint'),
>> installer=Distribution().fetch_build_egg)
>> zip_safe flag not set; analyzing archive contents...
>>
>> Installed /Users/rkern/betterprint-0.1-py2.5.egg
>> Out[4]: [betterprint 0.1 (/Users/rkern/betterprint-0.1-py2.5.egg)]
> 
> Okay, works for me, thanks. Is there an option to have the downloaded 
> module installed into the "site-packages" directory (and not into the 
> current)?

No. This is a hack. If you need things installed properly, use a setup.py.

-- 
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