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.
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: - >>> import random
-
>>> print random.randint(1,6)
-
5
-
>>> random.randint = 6
-
>>> print random.randint(1,6)
-
Traceback (most recent call last):
-
File "<interactive input>", line 1, in ?
-
TypeError: 'int' object is not callable
-
>>> del random.randint
-
>>> print random.randint(1,6)
-
Traceback (most recent call last):
-
File "<interactive input>", line 1, in ?
-
AttributeError: 'module' object has no attribute 'randint'
-
>>>
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.
The scrip is not seriously difficult really. It consists of two lines: -
import random
-
print random.randint(1, 6)
-
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 scrip is not seriously difficult really. It consists of two lines: -
import random
-
print random.randint(1, 6)
-
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.
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.
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: - import sys
-
for p in sys.path:
-
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. - >>> import random
-
This is module random.
-
>>> random.randint(1,6)
-
Traceback (most recent call last):
-
File "<interactive input>", line 1, in ?
-
AttributeError: 'module' object has no attribute 'randint'
-
>>>
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.
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: - import sys
-
for p in sys.path:
-
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. - >>> import random
-
This is module random.
-
>>> random.randint(1,6)
-
Traceback (most recent call last):
-
File "<interactive input>", line 1, in ?
-
AttributeError: 'module' object has no attribute 'randint'
-
>>>
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.
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"
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!
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.
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.
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.
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'
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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):...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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...
|
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...
| |