[SciPy-dev] Moving random.py

Fernando Perez Fernando.Perez at colorado.edu
Sat Dec 17 17:33:10 CST 2005

Pearu Peterson wrote:

>>What do you think Pearu?  You're probably the best one suited to make it
> Instead of
>    import scipy
>    scipy.import_all()
> we could have
>    import scipy.import_all
> or similar such as `import scipy.all` etc.
> Though having a function import_all() would have an advantage of 
> specifying packages one wishes to see in scipy namespace: 
> import_all('linalg','integrate') would import only scipy.linalg and 
> scipy.integrate packages and their dependencies if there are any.
> import_all() would import all scipy packages. Hmm, may be scipy.import_all 
> should read as scipy.import and scipy.import() would be 
> scipy.import('all'). However, using Python keyword `import` as a function 
> name would be showstopper here, just import_all does not sound right when 
> it could have optional arguments. Suggestions for better names are 
> welcome.

What I don't like about having a module that magically, upon import, rebuilds 
the scipy namespace, is the silent overloading of the default python semantics 
for the import statement.  It's one more thing to explain: 'this magic module 
does this and that under the hood just from being imported, ...'.

I think that a function approach is clearer and cleaner: its docstring can 
explicitly specify what it does and doesn't.  Thinking of names, I also think 
it's better to rename it something other than 'import*', so that we 
distinguish the fact that it's more than an import statement, but rather a 
namespeace loader.  Here's a suggested function header (rough cut):

def mod_load(*args):
     """Load one or more modules into scipy's top-level namespace.


       This function is intended to shorten the need to import many of scipy's
       submodules constantly with statements such as

       import scipy.linalg, scipy.fft, scipy.etc...

       Instead, you can say:

       import scipy



       to load all of them in one call.

       If a name which doesn't exist (except for 'all') in scipy's namespace 
       given, an exception [[WHAT? ImportError, probably?]] is raised.


       - the names (one or more strings) of all the scipy modules one wishes to
       load into the top-level namespace.

     If the single argument 'all' is given, then all of scipy's subpackages are


       The function returns a tuple with all the names of the modules which
       were actually imported.

And speaking of the 'all' module for interactive use, why can't we just 
populate the __all__ attribute of scipy's __init__.py without performing any 
actual imports?  With this, a regular

from scipy import *

will work, as everything listed in __all__ will get imported, but the plain 
'import scipy' will still be fast, since __all__ is just a list of strings, it 
doesn't contain the actual module objects.  Granted, with this you can't do

import scipy

but that's precisely why we have scipy.mod_load().

So in summary, unless I'm missing something, with:

1. mod_load as indicated above

2. __all__ properly populated

it seems to me we'd have all the desired usage cases covered, no?



More information about the Scipy-dev mailing list