AttributeError: 'module' object has no attribute 'choice'
AttributeError: 'module' object has no attribute 'choice'
我正在使用 python3。
首先,我在终端中使用 random.choice,它起作用了。
Python 3.2.3 (default, Feb 27 2014, 21:31:18)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> x = [1, 2, 3]
>>> random.choice(x)
3
但是当我在我的脚本中 运行 它时,我得到消息:
AttributeError: 'module' object has no attribute 'choice'
这是代码
import random
from scipy import *
from numpy import linalg as LA
import pickle
import operator
def new_pagerank_step(current_page, N, d, links):
print(links[current_page])
if random.rand() > 1 - d:
next_page = random.choice(links[current_page])
else:
next_page = random.randint(0, N)
return next_page
def pagerank_wikipedia_demo():
with open("wikilinks.pickle", "rb") as f:
titles, links = pickle.load(f)
current_page = 0
T = 1000
N = len(titles)
d = 0.4
Result = {}
result = []
for i in range(T):
result.append(current_page)
current_page = new_pagerank_step(current_page, N, d, links)
for i in range(N):
result.count(i)
Result[i] = result.count(i) / T
Sorted_Result = sorted(Result.items(), key=operator.itemgetter(1))
pagerank_wikipedia_demo()
这里,links[i]
(i
是一个整数)是一个列表。当我 运行 这个脚本时,它失败并显示上述消息。
我也检查过脚本的名称不是random
。而/usr/lib/python3.2/random.py
中只有一个名为random.py
的文件
为什么会这样?
您在此处使用 numpy.random
对象屏蔽了模块:
import random
from scipy import *
from scipy import *
导入带来了 一大堆名字,包括 random
:
>>> from scipy import *
>>> random
<module 'numpy.random' from '/Users/mj/Development/venvs/Whosebug-2.7/lib/python2.7/site-packages/numpy/random/__init__.pyc'>
这个替换了随机模块。
要么不使用通配符导入,要么导入 random
模块 在 从 scipy
.
导入所有内容之后
您也可以从 random
模块中导入 choice
并直接引用它,或者使用不同的导入名称绑定到:
from random import choice
from scipy import *
# use choice, not random.choice
或
import random as stdlib_random
from scipy import *
# use stdlib_random.choice, not random.choice
除了 Martijin Pieters 的回答之外,我想补充一点,您还可以使用别名导入 random
模块:
import random as rdm
from scipy import *
# Then you can
rdm.choice(some_sequence)
我正在使用 python3。 首先,我在终端中使用 random.choice,它起作用了。
Python 3.2.3 (default, Feb 27 2014, 21:31:18)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> x = [1, 2, 3]
>>> random.choice(x)
3
但是当我在我的脚本中 运行 它时,我得到消息:
AttributeError: 'module' object has no attribute 'choice'
这是代码
import random
from scipy import *
from numpy import linalg as LA
import pickle
import operator
def new_pagerank_step(current_page, N, d, links):
print(links[current_page])
if random.rand() > 1 - d:
next_page = random.choice(links[current_page])
else:
next_page = random.randint(0, N)
return next_page
def pagerank_wikipedia_demo():
with open("wikilinks.pickle", "rb") as f:
titles, links = pickle.load(f)
current_page = 0
T = 1000
N = len(titles)
d = 0.4
Result = {}
result = []
for i in range(T):
result.append(current_page)
current_page = new_pagerank_step(current_page, N, d, links)
for i in range(N):
result.count(i)
Result[i] = result.count(i) / T
Sorted_Result = sorted(Result.items(), key=operator.itemgetter(1))
pagerank_wikipedia_demo()
这里,links[i]
(i
是一个整数)是一个列表。当我 运行 这个脚本时,它失败并显示上述消息。
我也检查过脚本的名称不是random
。而/usr/lib/python3.2/random.py
random.py
的文件
为什么会这样?
您在此处使用 numpy.random
对象屏蔽了模块:
import random
from scipy import *
from scipy import *
导入带来了 一大堆名字,包括 random
:
>>> from scipy import *
>>> random
<module 'numpy.random' from '/Users/mj/Development/venvs/Whosebug-2.7/lib/python2.7/site-packages/numpy/random/__init__.pyc'>
这个替换了随机模块。
要么不使用通配符导入,要么导入 random
模块 在 从 scipy
.
您也可以从 random
模块中导入 choice
并直接引用它,或者使用不同的导入名称绑定到:
from random import choice
from scipy import *
# use choice, not random.choice
或
import random as stdlib_random
from scipy import *
# use stdlib_random.choice, not random.choice
除了 Martijin Pieters 的回答之外,我想补充一点,您还可以使用别名导入 random
模块:
import random as rdm
from scipy import *
# Then you can
rdm.choice(some_sequence)