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

josef.pktd at gmail.com josef.pktd at gmail.com
Wed Feb 16 12:54:18 EST 2011


On Wed, Feb 16, 2011 at 12:31 PM, Robert Kern <robert.kern at gmail.com> wrote:
> 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)

Thanks, this explanation is very useful

if I add  import scikits to get the namespace imports out of the way,
and use the None filter, then scipy.stats imports only 132 modules

>python -i stats_imports_1.py
len(beforenp), len(beforesp), len(beforestats), len(after)
81 160 242 374
['_bisect',
 'bisect',
 'inspect',
 'numpy.dual',
 'scipy.integrate',
 'scipy.lib',
 'scipy.linalg',
 'scipy.misc',
 'scipy.optimize',
 'scipy.sparse',
 'scipy.special',
 'scipy.stats',
 'swig_runtime_data4',
 'token',
 'tokenize']

Josef

>
> --
> 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
> _______________________________________________
> SciPy-Dev mailing list
> SciPy-Dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-dev
>



More information about the SciPy-Dev mailing list