By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,400 Members | 903 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,400 IT Pros & Developers. It's quick & easy.

Randint annoys me

P: 52
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.
May 28 '07 #1
Share this Question
Share on Google+
14 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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.
I can get the same message in this scenario:
Expand|Select|Wrap|Line Numbers
  1. >>> import random
  2. >>> print random.randint(1,6)
  3. 5
  4. >>> random.randint = 6
  5. >>> print random.randint(1,6)
  6. Traceback (most recent call last):
  7.   File "<interactive input>", line 1, in ?
  8. TypeError: 'int' object is not callable
  9. >>> del random.randint
  10. >>> print random.randint(1,6)
  11. Traceback (most recent call last):
  12.   File "<interactive input>", line 1, in ?
  13. AttributeError: 'module' object has no attribute 'randint'
  14. >>> 
May 28 '07 #2

bartonc
Expert 5K+
P: 6,596
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.
Posting the code here (USING [code] TAGS) would be very helpful.
May 28 '07 #3

P: 52
The scrip is not seriously difficult really. It consists of two lines:

Expand|Select|Wrap|Line Numbers
  1. import random
  2. print random.randint(1, 6)
  3.  
output in cmd:

Traceback (most recent call last):
File "C:\blah\blah\random.py", line 1, in <module>
import random
File "C:\blah\blah\random.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!
May 28 '07 #4

bartonc
Expert 5K+
P: 6,596
The scrip is not seriously difficult really. It consists of two lines:

Expand|Select|Wrap|Line Numbers
  1. import random
  2. print random.randint(1, 6)
  3.  
output in cmd:

Traceback (most recent call last):
File "C:\blah\blah\random.py", line 1, in <module>
import random
File "C:\blah\blah\random.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!
The "C:\blah\blah\random.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.
May 28 '07 #5

P: 52
The "C:\blah\blah\random.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.
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?

C:\programfiler\python script\random.py

Sorry for being so lame.
May 28 '07 #6

bvdet
Expert Mod 2.5K+
P: 2,851
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?

C:\programfiler\python script\random.py

Sorry for being so lame.
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:
Expand|Select|Wrap|Line Numbers
  1. import sys
  2. for p in sys.path:
  3.     print p
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.
Expand|Select|Wrap|Line Numbers
  1. >>> import random
  2. This is module random.
  3. >>> random.randint(1,6)
  4. Traceback (most recent call last):
  5.   File "<interactive input>", line 1, in ?
  6. AttributeError: 'module' object has no attribute 'randint'
  7. >>> 
May 28 '07 #7

bartonc
Expert 5K+
P: 6,596
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?

C:\programfiler\python script\random.py

Sorry for being so lame.
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.

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.
May 28 '07 #8

P: 52
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:
Expand|Select|Wrap|Line Numbers
  1. import sys
  2. for p in sys.path:
  3.     print p
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.
Expand|Select|Wrap|Line Numbers
  1. >>> import random
  2. This is module random.
  3. >>> random.randint(1,6)
  4. Traceback (most recent call last):
  5.   File "<interactive input>", line 1, in ?
  6. AttributeError: 'module' object has no attribute 'randint'
  7. >>> 
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.
May 28 '07 #9

bartonc
Expert 5K+
P: 6,596
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.
But I'll bet that one of the first ones was r"C:\\programfiler\\python script"
May 28 '07 #10

P: 52
Though 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!
May 28 '07 #11

bartonc
Expert 5K+
P: 6,596
Though 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!
That's what we're here for. Drop on in any old time.
May 28 '07 #12

bvdet
Expert Mod 2.5K+
P: 2,851
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.
The printed lines are names of directories, not files. I am glad that you solved your problem, and maybe learned something.
May 28 '07 #13

P: 2
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.
Aug 5 '10 #14

P: 2
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'
Aug 5 '10 #15

Post your reply

Sign in to post your reply or Sign up for a free account.