473,322 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Randint annoys me

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
14 8723
bvdet
2,851 Expert Mod 2GB
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
6,596 Expert 4TB
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
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
6,596 Expert 4TB
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
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
2,851 Expert Mod 2GB
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
6,596 Expert 4TB
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
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
6,596 Expert 4TB
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
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
6,596 Expert 4TB
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
2,851 Expert Mod 2GB
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
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
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

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

Similar topics

5
by: tgiles | last post by:
Hi, all. I'm YAPN (Yet Another Python Newbie), started messing with it last night and so far, so good. Documentation exellent and the people seem friendly enough ;) Ok, I started playing...
0
by: sector119 | last post by:
I use python 2.4.1 and PIL 1.1.5 and when I execute my program I got error: ../code.py Traceback (most recent call last): File "./code.py", line 7, in ? class DrawPlus(ImageDraw.Draw):...
3
by: TPJ | last post by:
"The advantage of xrange() over range() is minimal (since xrange() still has to create the values when asked for them) except when a very large range is used on a memory-starved machine or when all...
15
by: Steven Macintyre | last post by:
Hi all, I need to retrieve an integer from within a range ... this works ... below is my out puts ... it just does not seem so random ... Is there perhaps a suggestion out there to create a...
3
by: xZaft | last post by:
#Game by Todd Lunter from random import choice as _choice import random as r c = 'a' level = 1 stre = 2 spd = 2 exp = 0
8
by: jumperbl | last post by:
Hi, I am a noob and I was wondering if there is way to generate random mixture of alphabets and numbers? For example: 1234567890ABCDEFG or 4BA92ABD293BC3890 the length needs to be 17...
2
by: Georgy Panterov | last post by:
I am a relatively new python user. I am writing an economic simulation of acard-game. The simulation runs fine for a few iteration but then it gives an error of "list index out of range." The...
5
by: jcl43 | last post by:
Ok...so I have this psedecode that I want to follow but it keeps giving me an error so I'm not sure where I went wrong. Here is the psedecode: repeat n times generate random...
21
by: imran akhtar | last post by:
hi, i am making black jack code, but i am stuck, i have made start, but for reason gettin errors, which i dont seem be able to fix, below is my code wht i started. import random deck = *4...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.