为什么python中出现这个AttributeError? [英] Why does this AttributeError in python occur?

查看:2001
本文介绍了为什么python中出现这个AttributeError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一件事,我不明白。

为什么这个

import scipy # happens with several other modules, too. I took scipy as an example now...

matrix = scipy.sparse.coo_matrix(some_params)

产生此错误:

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


推荐答案

这是因为 scipy 模块没有任何名为稀疏的属性。只有在 import scipy.sparse 时才会定义该属性。

This happens because the scipy module doesn't have any attribute named sparse. That attribute only gets defined when you import scipy.sparse.

当您刚刚导入子模块时,子模块不会自动导入 import scipy ;你需要明确导入它们。大多数软件包也是如此,尽管软件包可以根据需要选择导入自己的子模块。 (例如,如果 scipy / __ init __。py 包含一个语句 import scipy.sparse scipy 时,都会导入稀疏子模块。)

Submodules don't automatically get imported when you just import scipy; you need to import them explicitly. The same holds for most packages, although a package can choose to import its own submodules if it wants to. (For example, if scipy/__init__.py included a statement import scipy.sparse, then the sparse submodule would be imported whenever you import scipy.)

这篇关于为什么python中出现这个AttributeError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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