import random module

Benjamin Niemann pink at odahoda.de
Wed Mar 22 11:58:23 EST 2006


DataSmash wrote:

> Hi,
> When I import the random module at the python interpreter, it works
> fine:
>>>> import random
>>>> x = random.randint(1,55)
>>>> print x
> 14
>>>>
> 
> BUT, when I put the same code in a python script:
> * random.py:
> 
> import random
> 
> x = random.randint(1,55)
> print x
> 
> and run it at the command line, I get:
> Traceback (most recent call last):
>    File p:\temp\random.py, line 7, in ?
>       import random
>    File p:\temp\random.py, line 8, in ?
>       x = random.randint(1,55)
> AttributeError: 'module" object has no attribut 'randint'
> 
> I run scripts at the command line everyday so there must be something
> specifically
> wrong with the "random" module, unless I'm totally missing something
> here.

This is not specific to the random module, try:

time.py:
----------------
import time
print time.time()
----------------

Don't name your script 'random.py' (or any other name from the stdlib).
'import random' will import the script itself (not the random module from
the stdlib), which is not what you want.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/



More information about the Python-list mailing list