Hello, I am rather a newbie in python, and I'm trying to creawte a simple program that uses random. When I import random in the python program, it runs along smoothly. However, when I import it in a script, and then use print random.randint( 1,6), it tells me: "module object has no attribute 'randint'". What in the blazes am I supposed to make of that? My friend is sitting not ten inches away from me, using the same script, and it works like a charm.
Randint annoys me
Collapse
X
-
I can get the same message in this scenario:[code=Python]>>> import randomOriginally posted by BurnTardHello, I am rather a newbie in python, and I'm trying to creawte a simple program that uses random. When I import random in the python program, it runs along smoothly. However, when I import it in a script, and then use print random.randint( 1,6), it tells me: "module object has no attribute 'randint'". What in the blazes am I supposed to make of that? My friend is sitting not ten inches away from me, using the same script, and it works like a charm.
>>> print random.randint( 1,6)
5
>>> random.randint = 6
>>> print random.randint( 1,6)
Traceback (most recent call last):
File "<interacti ve input>", line 1, in ?
TypeError: 'int' object is not callable
>>> del random.randint
>>> print random.randint( 1,6)
Traceback (most recent call last):
File "<interacti ve input>", line 1, in ?
AttributeError: 'module' object has no attribute 'randint'
>>> [/code] -
Posting the code here (USING [CODE] TAGS) would be very helpful.Originally posted by BurnTardHello, I am rather a newbie in python, and I'm trying to creawte a simple program that uses random. When I import random in the python program, it runs along smoothly. However, when I import it in a script, and then use print random.randint( 1,6), it tells me: "module object has no attribute 'randint'". What in the blazes am I supposed to make of that? My friend is sitting not ten inches away from me, using the same script, and it works like a charm.Comment
-
The scrip is not seriously difficult really. It consists of two lines:
output in cmd:Code:import random print random.randint(1, 6)
Traceback (most recent call last):
File "C:\blah\blah\r andom.py", line 1, in <module>
import random
File "C:\blah\blah\r andom.py", line 2, in <module>
print random.randint( 1,6)
AttributeError: 'module' object has no attribute 'randint'
If anyone has an actual solution, please help me!Comment
-
The "C:\blah\blah\r andom.py" part may actually be of interest. If there is another module in the sys.path by the name of "random" which is being found first (before the library module named "random"), that would happen.Originally posted by BurnTardThe scrip is not seriously difficult really. It consists of two lines:
output in cmd:Code:import random print random.randint(1, 6)
Traceback (most recent call last):
File "C:\blah\blah\r andom.py", line 1, in <module>
import random
File "C:\blah\blah\r andom.py", line 2, in <module>
print random.randint( 1,6)
AttributeError: 'module' object has no attribute 'randint'
If anyone has an actual solution, please help me!Comment
-
Please could you translate that into a language I can understand? I am new to the stuff after all. Did I interpet it correctly if I think that was a request for the following?Originally posted by bartoncThe "C:\blah\blah\r andom.py" part may actually be of interest. If there is another module in the sys.path by the name of "random" which is being found first (before the library module named "random"), that would happen.
C:\programfiler \python script\random.p y
Sorry for being so lame.Comment
-
Barton was telling you that Python may have found another file named 'random.py' before it found the Python library file 'random.py'. That would explain the problem you are having. Run this script to check your path:[code=Python]import sysOriginally posted by BurnTardPlease could you translate that into a language I can understand? I am new to the stuff after all. Did I interpet it correctly if I think that was a request for the following?
C:\programfiler \python script\random.p y
Sorry for being so lame.
for p in sys.path:
print p[/code]This is the directory order Python searches for files to import. If __init__.py does not exist in the directory, it is skipped. I placed a file 'random.py' in one of the first directories in my sys.path. It contains a simple print statement.[code=Python]>>> import random
This is module random.
>>> random.randint( 1,6)
Traceback (most recent call last):
File "<interacti ve input>", line 1, in ?
AttributeError: 'module' object has no attribute 'randint'
>>> [/code]Comment
-
That's the problem! You have a module named random.py which is in python's search path. (too much detail: That path can be viewed and changed through sys.path). When you need something from the library, python goes out and searches sys.path in the order of the items in that list. Your module is being found instead to the library module.Originally posted by BurnTardPlease could you translate that into a language I can understand? I am new to the stuff after all. Did I interpet it correctly if I think that was a request for the following?
C:\programfiler \python script\random.p y
Sorry for being so lame.
The best fix is to change the name of your "random.py" file and NEVER name your own work with names from the standard library unless you really want to replace that functionality.Comment
-
I did that thing with checking the system path, and it came up with a bunch of lines, but not one of them were/had the word random in them.Originally posted by bvdetBarton was telling you that Python may have found another file named 'random.py' before it found the Python library file 'random.py'. That would explain the problem you are having. Run this script to check your path:[code=Python]import sys
for p in sys.path:
print p[/code]This is the directory order Python searches for files to import. If __init__.py does not exist in the directory, it is skipped. I placed a file 'random.py' in one of the first directories in my sys.path. It contains a simple print statement.[code=Python]>>> import random
This is module random.
>>> random.randint( 1,6)
Traceback (most recent call last):
File "<interacti ve input>", line 1, in ?
AttributeError: 'module' object has no attribute 'randint'
>>> [/code]Comment
-
But I'll bet that one of the first ones was r"C:\\programfi ler\\python script"Originally posted by BurnTardI did that thing with checking the system path, and it came up with a bunch of lines, but not one of them were/had the word random in them.Comment
-
That's what we're here for. Drop on in any old time.Originally posted by BurnTardThough apparently you were right, if I understood. The minute I changed the name from random.py to randomname.py, it worked perfectly! Thank you guys so much!Comment
-
The printed lines are names of directories, not files. I am glad that you solved your problem, and maybe learned something.Originally posted by BurnTardI did that thing with checking the system path, and it came up with a bunch of lines, but not one of them were/had the word random in them.Comment
-
Well this is all very fine - but where to look if renaming the file doesn't make a bit of difference.
I made a file random.py (duh) on Tiger Mac with Python 2.6.7
It tells me there is no randint in the module - but I have no file named random anywhere....
maybe in memory.
BRB when I restart this apple.Comment
-
Ahhh.. this is because on Mac (maybe on a win box too) Python compiles the module you import in your script into a file called 'yourmodule.pyc ' - I found my useless random.pyc file and deleted it.
No more 'trubble ert mill'Comment
Comment