[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Order in which modules are imported

tkpmep@hotmail.com

2/22/2008 11:22:00 PM

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


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

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(random)
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.
>>> from pylab import *
>>> import random
>>> random.uniform(0,1)
0.75262941795069283
>>> help(random)
Help on module random:

NAME
random - Random variable generators.

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




3 Answers

Roy Smith

2/22/2008 11:46:00 PM

0

In article
<8f6dc46e-2f15-4d7a-961b-15cad7255a38@64g2000hsw.googlegroups.com>,
tkpmep@hotmail.com wrote:

> 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
>
>
> >>> import random
> >>> from pylab import *
> >>> x = random.uniform(0,1)
>
> 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(random)
> 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.

Tim Chase

2/22/2008 11:48:00 PM

0

>>>> import random
>>>> from pylab import *
>>>> x = random.uniform(0,1)
>
> Traceback (most recent call last):


I suspect that

>>> 'random' in dir(pylab)

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



Dave Hansen

2/22/2008 11:49:00 PM

0

On Feb 22, 5:21 pm, tkp...@hotmail.com wrote:
> 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,

[...]
>
> >>> import random
> >>> from pylab import *

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