Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this python program is not working? AttributeError: 'module' object has no attribute

Tags:

python

random

I wrote a very simple python program.

#!/usr/bin/env python
import random
x = random.uniform(-1, 1)
print str(x)

I run this from command prompt.

python random.py

It returned with error:

Traceback (most recent call last):
  File "random.py", line 2, in <module>
    import random
  File "D:\python practise\random.py", line 3, in <module>
    x = random.uniform(-1, 1)
AttributeError: 'module' object has no attribute 'uniform'

It is a very simple program, I can't understand what mistake I did in this.

(operating system: Windows 7; python version: 2.7)

like image 235
narayanpatra Avatar asked Jan 21 '11 16:01

narayanpatra


People also ask

How do I fix module object has no attribute?

To solve the Python "AttributeError: module has no attribute", make sure you haven't named your local modules with names of remote modules, e.g. datetime.py or requests.py and remove any circular dependencies in import statements.

Why am I getting AttributeError object has no attribute?

If you are getting an object that has no attribute error then the reason behind it is because your indentation is goofed, and you've mixed tabs and spaces.

How do I fix AttributeError in Python?

Solution for AttributeError Errors and exceptions in Python can be handled using exception handling i.e. by using try and except in Python. Example: Consider the above class example, we want to do something else rather than printing the traceback Whenever an AttributeError is raised.

What does list object has no attribute mean Python?

The Python "AttributeError: 'list' object has no attribute" occurs when we access an attribute that doesn't exist on a list. To solve the error, access the list element at a specific index or correct the assignment.


3 Answers

Don't name your file random.py, it is importing itself and looking for uniform in it.

It's a bit of a quirk with how Python imports things, it looks in the local directory first and then starts searching the PYTHONPATH. Basically, be careful naming any of your .py files the same as one of the standard library modules.

like image 134
Daniel DiPaolo Avatar answered Oct 18 '22 21:10

Daniel DiPaolo


Don't name your program as an Library. And just as a Tip: You don't need an String storing something and printing it out just after generating it.

#!/usr/bin/env python
import random
print(random.uniform(-1, 1))

This will work fine too ;)

like image 22
woodleader Avatar answered Oct 18 '22 22:10

woodleader


Your problem is that you named your test program "random.py". The current working directory is on the module search path before anything else, so when you say "import random", it imports your own test program rather than the standard library random.

Rename your test program -- or just take the .py suffix off -- and it should work.

like image 32
zwol Avatar answered Oct 18 '22 21:10

zwol