为什么这个python程序不起作用? AttributeError:“模块"对象没有属性 [英] Why this python program is not working? AttributeError: 'module' object has no attribute
问题描述
我写了一个非常简单的python程序.
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
它返回错误:
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.
(操作系统:Windows 7; python版本:2.7)
(operating system: Windows 7; python version: 2.7)
推荐答案
不要命名您的文件random.py
,它是在导入自身并在其中寻找uniform
.
Don't name your file random.py
, it is importing itself and looking for uniform
in it.
Python如何导入东西有点古怪,它首先在本地目录中查找,然后开始搜索PYTHONPATH
.基本上,要小心地将您的任何.py
文件命名为与标准库模块之一相同.
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.
这篇关于为什么这个python程序不起作用? AttributeError:“模块"对象没有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!