Order in which modules are imported

T

tkpmep

I imported two modules (random and matplotlib), and found that the
functions available to me from the random module depended on the order
in which the imports occured. In particular, if I import random first,
none of its functions seem to be available, while if I import it after
matplotlib, I seem to have access to all of its functions. What could
the problem be?

Thomas Philips


Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
x = random.uniform(0,1)
AttributeError: 'builtin_function_or_method' object has no attribute
'uniform'Help on built-in function random_sample:

random_sample(...)
Return random floats in the half-open interval [0.0, 1.0).

random_sample(size=None) -> random values


In sharp contrast, I get what I expect when I reverse the order of the
imports.Help on module random:

NAME
random - Random variable generators.

FILE
c:\python25\lib\random.py
..
..
..
 
R

Roy Smith

I imported two modules (random and matplotlib), and found that the
functions available to me from the random module depended on the order
in which the imports occured. In particular, if I import random first,
none of its functions seem to be available, while if I import it after
matplotlib, I seem to have access to all of its functions. What could
the problem be?

Thomas Philips



Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
x = random.uniform(0,1)
AttributeError: 'builtin_function_or_method' object has no attribute
'uniform'
Help on built-in function random_sample:

I'm guessing the module pylab has a symbol in it named "random", which is
over-writing the name "random" you created with the first import.

Try changing it to:

import random
import pylab

and then referring to the things in pylab by their qualified
(pylab.whatever) names.
 
T

Tim Chase

import random
Traceback (most recent call last):


I suspect that

returns True...this would be one of those reasons that "from
<module> import *" is scowled upon. You have to know what
<module> is dumping into your namespace, or otherwise, it will
likely tromp atop other things you have defined before.

Looking at the source of pylab.py, I see

from numpy.random import *

and numpy.random has a "random" function in it. This waltzes
atop your random module. A modern fandango-on-core...perhaps
"fandango on namespace".

The other alternative, if you must do "from pylab import *",
would be to import the system "random" module under another name:

import random as pyrandom
from pylab import *
...
x = pyrandom.uniform(0,1)

-tkc
 
D

Dave Hansen

I imported two modules (random and matplotlib), and found that the
functions available to me from the random module depended on the order
in which the imports occured. In particular, if I import random first,
[...]

Change this --^ lint to "import pylab" and see what happens. Of
course, that may force other changes in your script...

And remember that "from <module> import *" is never your friend.

Regards,

-=Dave
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top