6

As you may know from my previous posts, I'm learning Python. And this time I have a small error which I think is with this build of Python itself. When using the following:

import random
number = random.randint(1,10000)

Python gives me this error:

File "C\Users\name\Documents\Python\random.py", line 5, in (module)
  print random.random()
TypeError: 'module' object is not callable

Every time I try to run it. Me no understand. Any help would be much appreciated!

EDIT: The two lines of code I'm trying to run:

import random
print random.randint(1,100)

That's it. And it gives me the same error.

9
  • Are you importing the module twice? Commented Dec 11, 2012 at 3:26
  • 11
    You can't call your script the same name. Change it to my_random.py or something else. Commented Dec 11, 2012 at 3:28
  • @squiguy Thanks, but that didn't seem to work. Commented Dec 11, 2012 at 3:29
  • @enginefree Nope, just once: Commented Dec 11, 2012 at 3:30
  • 1
    Please post all of your code. After reading your error statement it looks like you are trying to print random.random() which does not exist. Do you mean print number? Commented Dec 11, 2012 at 3:32

5 Answers 5

19

By naming your script random.py, you've created a naming conflict with the random standard library module.

When you try to run your script, the directory containing the script will be added to the start of the module import path. So when your script does import random, you're effectively running a second copy of the script as the random module.

When the random module runs import random, it means that random.random will also be a reference to your module. So when you attempt to call the random.random() standard library function, you're actually attempting to call the module object resulting in the error you got.

If you rename your script to something else, the problem should go away.

Sign up to request clarification or add additional context in comments.

2 Comments

I actually think this was the problem. While this actual post didn't help me, it is what I did to fix it, I believe. Answer accepted.
wow:) after a quite long time of struggling with random module thing This finally made my day:) Thank you!
0

Even I faced the same problem. I have renamed my python file from random.py to shuffle.py. This didn't work out. Then I changed the version then it worked. This may help a bit. Python version : 3.6.7 replace import random; to import random2;

Comments

0

The simple answer: Change your filename from "random.py" to something else as it is conflicting with random library.

Comments

-1

I am using pycharm and I had to take the additional step to import the methods from random. In my case:

import random
 from random import choice

Comments

-1

You can install random2 by following command : pip install random2 Copy the code below :

import random2
from random2 import choice,randint

list = [1,2,3,4,5 ]
print(random2.choice(list))
print(random2.randint(1,100))

It should work..

1 Comment

No, don't do this. That package hasn't been updated in over a decade. Its description also contains typos and unclear descriptions... I wouldn't go anywhere near it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.