2

New to python and trying to do a random number generator. However, I am having trouble importing the random module. I get a AttributeError when I try to use anything from the random module. Thanks for your help.

#!/usr/bin/python -tt

import random

def main():
  x = random.randint(1,1000)
  print x

if __name__ == '__main__':
  main()
7
  • 4
    What is the exact error? This code is fine. Commented Jan 5, 2011 at 7:25
  • Removed [random-number-generator] because this isn't a question about random number generators. It's just about importing modules. Commented Jan 5, 2011 at 7:26
  • Here is the error: Traceback (most recent call last): File "./random.py", line 10, in <module> main() File "./random.py", line 6, in main x = random.randint(1,1000) AttributeError: 'module' object has no attribute 'randint' Commented Jan 5, 2011 at 7:28
  • You may also run the Python Interpreter interactively and do a import random and see what happens. Commented Jan 5, 2011 at 7:28
  • 1
    In this case, read TryPyPy's answer. Commented Jan 5, 2011 at 7:28

4 Answers 4

9

You probably have a file named random.py (or pyc) in your current directory. You can find out where the random module you're using comes from by doing this:

import random
print(random.__file__)
Sign up to request clarification or add additional context in comments.

5 Comments

FWIW, apparently on windows machines, this won't give the full path under some circumstances.
Traceback (most recent call last): File "./anothername.py", line 10, in <module> main() File "./anothername.py", line 6, in main x = random.randint(1,1000) AttributeError: 'module' object has no attribute 'randint'
@Empty Monty: Put the suggested code first in your module. See what it prints. Most likely you forgot to delete the random.pyc file.
Have you moved/deleted both the file named random.py (plain code) and random.pyc (bytecode compiled)? If so, my answer is wrong...
That fixed it. I printed out the location of the file, and say it came from the same directory, probably made after running random.py. I trashed the pyc file, renamed it to something else, printed out the location of the file and it was from the the python library. Silly python rookie mistake. Thanks so much for your help! Thanks so much.
3

The python importing system vaguely works as follows.

  1. A line like import foo is executed.
  2. Python looks through the directories in sys.path which is a list in the order in which they occur. The first entry in sys.path is the directory in which the main file lives.
  3. When Python finds a file named "foo.py", it executes it and places the global namespace of that file in the module sys.modules['foo'].
  4. Python binds that module to the name foo in the scope in which the original import occurs.

So when you name the file random.py, python finds that file before it searches through the files in the standard library. You are "shadowing" the random module with your file.

This is simplified and doesn't give the full picture. For example, it ignores .pyc files.

1 Comment

Thanks for the detailed answer. This is all making sense now.
2

Okay, dont name your python program as random.py name it as something else. The interpreter is getting confused by its module and the your program.

Comments

0

I solved the same problem reading this post. I had my file named random.py Wouldn't it be better to start filename with capital letters so it doesn't match with python module, cos I rookie like me wouldn't be familiar with many python modules. Thanks

Comments

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.