problems loading modules


F

Frank

Hi,

I have the following weird behavior when I load modules:


Everything is fine.
Traceback (most recent call last):

Here it does not work.


Now everything is back to normal.

That means the order the modules are loaded matters! I would expect
there is a problem with my installation because I guess this should
normally be independent of the loaded modules.

Here are my questions:
1. Does anyone has this behavior too?

2. How can I fix this problem?

I use linux with fedora core 6 and python 2.4.4

I appreciate any hint. Thanks!

Frank
 
Ad

Advertisements

R

Robert Kern

Frank said:
Hi,

I have the following weird behavior when I load modules:


8

Everything is fine.

Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'module' object has no attribute 'randrange'

Here it does not work.

numpy has a subpackage called random. You imported it when you did "from numpy
import *".

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

Ben Finney

Frank said:
Everything is fine.

Traceback (most recent call last):


Here it does not work.

"Don't do that, then."

More specifically, 'from foo import *' is deprecated for exactly the
reason you found here: you risk clobbering an existing name in the
current namespace, and there's no way to determine that by looking at
the code.

Instead, import modules preserving a module namespace, which is the
behaviour you get from 'import foo'. That way, all names remain
explicit and you can see where you might be re-binding an existing
name.
<module 'numpy.random' from '/usr/lib/python2.4/site-packages/numpy/random/__init__.pyc'>

Alternatively, if you want *specific* attributes from a module or
package to be in the current namespace, import them explicitly by
name; the same applied above, that you can see which names in
particular are being re-bound.
<module 'numpy.random' from '/usr/lib/python2.4/site-packages/numpy/random/__init__.pyc'>

Again: don't use 'from foo import *', without knowing exactly why
you're doing it. 'import foo' or 'from foo import bar' are always
available, and usually better.
 
Ad

Advertisements


Top