[SciPy-Dev] Breaking up scipy.stats or How to avoid importing the kitchen sink (when we are not in the kitchen)

Robert Kern robert.kern at gmail.com
Wed Feb 16 12:31:12 EST 2011


On Wed, Feb 16, 2011 at 09:23,  <josef.pktd at gmail.com> wrote:

> I don't understand some things about the imports,
> why do I get some distutils and enthought modules with the stats
> import. (I don't understand the lazy import machinery.)

There is no lazy import machinery at work here. scipy.stats imports
scipy.sparse (through an intermediate, probably scipy.linalg).
scipy.sparse tries to import scikits.umfpack to provide optional
functionality. scikits is a namespace package, which uses
pkg_resources to implement that namespace behavior. The way that
happens to be implemented in your version of pkg_resources is to
import all namespace packages the first time you import any namespace
package (just their mostly-empty __init__.py files, not the actual
code). It's probably using some support code from distutils and
pkgutil in the process. This is also why you get mpl_toolkits, paste,
and scikits.

By the way, you might want to use this function to get the set of
module names that have been imported at a particular time. It handles
an important subtlety that can confuse the results.

  def get_modules():
    return set(m for m in sys.modules if sys.modules[m] is not None)

-- 
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 SciPy-Dev mailing list