简体   繁体   English

AttributeError:“模块”对象没有属性“选择”

[英]AttributeError: 'module' object has no attribute 'choice'

I am using python3. 我正在使用python3。 First I use random.choice in Terminal, and it works. 首先,我在Terminal中使用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

But when I run it in my script, I get the message: 但是,当我在脚本中运行它时,我得到了消息:

 AttributeError: 'module' object has no attribute 'choice' 

Here is the code 这是代码

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()

Here, links[i] ( i is an integer) is a list. 在这里, links[i]i是整数)是一个列表。 When I run this script, it fails with the message mentioned above. 当我运行此脚本时,它失败并显示上述消息。

I have also checked that the name of the script is not random . 我还检查了脚本的名称是否不是random And there is only one file named random.py in /usr/lib/python3.2/random.py /usr/lib/python3.2/random.py只有一个名为random.py文件

Why this happens? 为什么会这样?

You masked the module with the numpy.random object here: 您在此处使用numpy.random对象屏蔽了模块:

import random
from scipy import *

The from scipy import * import brings a whole lot of names , including random : from scipy import * import带来了很多名称 ,包括random

>>> from scipy import *
>>> random
<module 'numpy.random' from '/Users/mj/Development/venvs/stackoverflow-2.7/lib/python2.7/site-packages/numpy/random/__init__.pyc'>

This replaced the random module. 取代了随机模块。

Either don't use a wildcard import, or import the random module after importing everything from scipy . scipy导入所有内容 ,要么不使用通配符导入,要么导入random模块。

You could also import choice from the random module and refer to it directly, or use a different name for the import to bind to: 您还choicerandom模块中导入choice并直接引用它,或者对导入使用其他名称来绑定到:

from random import choice
from scipy import *

# use choice, not random.choice

or 要么

import random as stdlib_random
from scipy import *

# use stdlib_random.choice, not random.choice

In addition to Martijin Pieters answer I want to add that you could also import random module with an alias: 除了Martijin Pieters的答案,我想补充一点,您也可以导入带有别名的random模块:

import random as rdm
from scipy import *       

# Then you can 
rdm.choice(some_sequence)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM