[SciPy-user] egg dependency?

David M. Cooke cookedm at physics.mcmaster.ca
Fri Dec 2 23:30:44 CST 2005

Pearu Peterson <pearu at scipy.org> writes:

> 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
> try:
>      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.

Assuming that if the imports fail that they fail with ImportError, the result is
the same: have_setuptools is set to 0, and distutils.core.setup is
imported as old_setup. How would old_setup be polluted? It's
overwritten. Now, it *could* be possible that importing has side

This is where setuptools comes in handy, with its dependency checking
that doesn't rely on imports, but on metadata in eggs.

> So, I would write (not tested code follows)
> have_setuptools = False
> try:
>      import setuptools
>      try:
>          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
>      ...
> else:
>      from distutils.core import setup as old_setup
>      ...

Except for extra error messages, this is the same thing.

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

Because the original code (which John Hunter was running) didn't have
the bdist_egg import; I added that this afternoon.

|David M. Cooke                      http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at physics.mcmaster.ca

More information about the SciPy-user mailing list