pylab package dependencies

Robert Kern robert.kern at gmail.com
Tue Oct 17 13:39:07 EDT 2006


Lou Pecora wrote:
> In article <mailman.604.1161057667.11739.python-list at python.org>,
>  Robert Kern <robert.kern at gmail.com> wrote:
> 
>> I presume what you did was something like this:
>>
>>    from matplotlib import pylab
>>    [N,x] = hist(eig, 10)
>>
>> What you actually want is this:
>>
>>    from matplotlib import pylab
>>    [N,x] = pylab.hist(eig, 10)
>>
>> Or, if you're at the interactive prompt (but remember that it is inadvisable 
>> to 
>> do so in modules):
>>
>>    from matplotlib.pylab import *
>>    [N,x] = hist(eig, 10)
>>
>> You will probably want to review the section of the tutorial on importing 
>> modules if you don't understand the differences.
> 
> Is pylab part of matplotlib? 

Yes.

> I always thought it was the other way 
> around. I have a similar view of numpy as part of scipy. 

It is not.

> Maybe I'm 
> confused on the dependencies.  I find it confusing in the examples 
> sometimes when the "bigger" package is imported (e.g. scipy) and then a 
> "subpackage" is also imported.  Like this:
> 
> from scipi import *
> from scipi import numpy

The latter would definitely be bad form if it worked. numpy is a package all by 
itself and should be imported by itself.

> I know I've seen stuff like that, but I don't get it.  The dependencies 
> are confusing to me.  

pylab is a module provided with matplotlib that exposes a nice interface for 
certain purposes. Somewhat confusingly, it is provided in two places, as its own 
module:

   import pylab

and as a submodule in the matplotlib package:

   from matplotlib import pylab

Both do the same thing. You get to ask John Hunter if you want to know the whys 
and wherefores.

numpy is a package all by itself. scipy is a package all by itself although it 
depends on numpy being installed. You cannot import numpy from scipy. The 
dependency of scipy on numpy does *not* entail that scipy will provide numpy in 
its namespace.

Sometimes packages/modules are sloppy and accidentally expose the modules that 
they import. For example, if you had a module foo.py like this:

   import bar
   def dostuff():
     pass

then foo.py depends on bar.py. One *could* also do this:

   from foo import bar

However, as I said, this would be bad form. It is an accident that the bar 
module is exposed there. It should not be imported from the foo module.

Naturally, there are exceptions. Sometimes some other module is deliberately 
imported and intended to be exposed in that place. Hopefully, there is a comment 
to that effect explaining that it was intentional.

> I did a search of the tutorial on 'import' but didn't find the answer.

It certainly doesn't answer your questions, but it should answer the OP's if my 
presumptions are correct. Importing a module like so:

   import mymodule
   from mypackage import myothermodule

does not take all of symbols in mymodule and myothermodule and place them in the 
current namespace.

   http://docs.python.org/tut/node8.html

-- 
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 Python-list mailing list