noob questions
Steve Holden
steve at holdenweb.com
Tue Apr 24 12:26:03 EDT 2007
T.Crane wrote:
> I'm new to python and I seem to get different behavior depending on...
> well who knows what. Here's my question concerning importation of
> packages/modules.
>
> I want to use scipy. So at the prompt (using iPython, installed with
> Enthought edition on Windows XP) I type:
>
> ln [1]: from scipy import *
>
> Now, I know integrate is a package this is in scipy. I want to use
> the ode class/module that's in integrate. So I type:
>
> ln [2]: from integrate import *
>
> And I'm told
>
> ImportError: No module named integrate
>
> In order to get access to the ode class, I end up having to type:
>
> ln [3]: from scipy.integrate import *
>
> Then it works. Will someone explain to me what I'm misunderstanding?
> I don't understand why after importing everything in scipy (which
> includes integrate), I was told there was no module named integrate.
> What gives? I there are sevarl functions (modules) that I want to use
> that are a few levels down from the root package, what's the most
> economical method of importing them?
>
> Any clarification would be appreciated.
>
Remember that scipy is a *package*. So when you say you have "imported
everything in scipy" that's not necessarily the case. You have imported
the scipy package, which has run the __init__.py in the package's base
directory. This doesn't necessarily do anything for scipy.integrate.
The statement
from integrate import *
tells the interpreter to search its sys.path looking for a module or
package called integrate. Just having imported scipy doesn't change
sys.path to put subdirectories of the scipy package's base directory
onto the path.
from scipy.integrate import *
however, tells the interpreter to look for a module or package *inside a
module or package called scipy* that can be found in one of the
directories on sys.path. Since the first import worked, and because
scipy is a package not just a plain module, this correctly locates the
integrate module (or package).
By the way, be careful with those from xxx import * statements. If scipy
is specifically designed to support such usage then you should be OK,
but in general this can "pollute" your namespace by bringing in all the
names defined in the imported package or module within your local
namespace. This might overwrite bindings you have already made within
that namespace. Just a point to watch.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com
More information about the Python-list
mailing list