AttributeError: 'module' 对象没有属性 'choice' [英] AttributeError: 'module' object has no attribute 'choice'

查看:57
本文介绍了AttributeError: 'module' 对象没有属性 'choice'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 python3.首先,我在终端中使用 random.choice,它可以工作.

Python 3.2.3(默认,2014 年 2 月 27 日,21:31:18)[GCC 4.6.3] 在 linux2 上输入帮助"、版权"、信用"或许可"以获取更多信息.>>>随机导入>>>x = [1, 2, 3]>>>随机选择(x)3

但是当我在脚本中运行它时,我收到消息:

<块引用>

AttributeError: 'module' 对象没有属性 'choice'

这是代码

随机导入从 scipy 导入 *从 numpy 导入 linalg 作为 LA进口泡菜进口经营者def new_pagerank_step(current_page, N, d, links):打印(链接[current_page])如果 random.rand() >1 - 天:next_page = random.choice(links[current_page])别的:next_page = random.randint(0, N)返回下一页def pagerank_wikipedia_demo():使用 open("wikilinks.pickle", "rb") 作为 f:标题,链接 = pickle.load(f)当前_页面 = 0T = 1000N = len(标题)d = 0.4结果 = {}结果 = []对于范围(T)中的我:result.append(current_page)current_page = new_pagerank_step(current_page, N, d, links)对于范围(N)中的我:结果.count(i)结果[i] = result.count(i)/TSorted_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 对象屏蔽了模块:

随机导入从 scipy 导入 *

from scipy import * import 带来了一大堆名字,包括random:

<预><代码>>>>从 scipy 导入 *>>>随机的<来自'/Users/mj/Development/venvs/stackoverflow-2.7/lib/python2.7/site-packages/numpy/random/__init__.pyc'的模块'numpy.random'>

取代了随机模块.

要么不使用通配符导入,要么在从 scipy 导入所有内容之后导入 random 模块.

您也可以从 random 模块导入 choice 并直接引用它,或者使用不同的名称来绑定到导入:

来自随机导入选择从 scipy 导入 *# 使用选择,而不是 random.choice

import random 作为 stdlib_random从 scipy 导入 *# 使用 stdlib_random.choice,而不是 random.choice

I am using python3. First I use random.choice in Terminal, and it works.

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. 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. And there is only one file named random.py in /usr/lib/python3.2/random.py

Why this happens?

解决方案

You masked the module with the numpy.random object here:

import random
from scipy import *

The from scipy import * import brings a whole lot of names, including 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.

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:

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

这篇关于AttributeError: 'module' 对象没有属性 'choice'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆