noob questions

T

T.Crane

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.

trevis
 
S

Steve Holden

T.Crane said:
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
 
R

Robert Kern

T.Crane said:
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?

Everything Steve Holden said is correct. I'll just add that we do have a
function that will load all of the subpackages.

import scipy
scipy.pkgload()
from scipy import *

We don't load all of the subpackages by default because we have a lot of
extension modules that link to largish libraries. Loading all of them all of the
time takes substantial amounts of time.

Also, I don't recommend using either pkgload or "from scipy import *" in code.
However, from the interactive prompt, feel free. That is precisely what these
features were designed for.

--
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top