[SciPy-user] egg dependency?

Pearu Peterson pearu at scipy.org
Fri Dec 2 16:26:34 CST 2005

On Fri, 2 Dec 2005, Alan G Isaac wrote:

> On Fri, 2 Dec 2005, Pearu Peterson apparently wrote:
>> Without looking into details much, this demonstrates well
>> the evilness of using  `try: .. except: ..` blocks when
>> checking the availability of packages. Sure, it's very
>> convinient but there are always (or will be) cases where
>> this sugar fails (or will so).
> What is the preferred approach?

That's a good question and the answer will depend on a particular case.
However, there are few suggestions that will minimize the risk of
using try-except blocks:
  - the contents of try block should be minimal, even if then overall code
    will be more verbose.
  - except block should be as explicit as possible about what
    exceptions are expected, it should raise exception in all other cases.
  - and finally, don't use it when it is avoidable, it often is.

In scipy.distutils.core case I see the following problems. First, the code

     from setuptools import setup as old_setup
     # very old setuptools don't have this
     from setuptools.command import bdist_egg
     have_setuptools = 1
except ImportError:
     from distutils.core import setup as old_setup
     have_setuptools = 0

has a pitfall in the case when `from setuptools import setup as 
old_setup` succeeds but `from setuptools.command import bdist_egg` fails 
-- old_setup may be polluted.

So, I would write (not tested code follows)

have_setuptools = False
     import setuptools
         import setuptools.command.bdist_egg
         have_setuptools = True
     except ImportError, msg:
         print 'setuptools too old:',msg,'Ignoring.'
except ImportError, msg:
     print 'setuptools not available:',msg,'Ignoring.'

if have_setuptools:
     from setuptools import setup as old_setup
     from setuptools.command import bdist_egg
     from distutils.core import setup as old_setup

Second, it is not clear why the original code failed. It shouldn't have 
when reading the code. So I would investigate for possible reasons, 
and the code above might need some adjustments.


More information about the SciPy-user mailing list