繁体   English   中英

无法从模块导入Python numpy?

[英]Python numpy can't be imported from a module?

我的程序是:

import numpy as np
np.zeros(2)

它在带有__init__.py的文件夹中

如果我将其作为python a.py运行, python a.py得到:

Traceback (most recent call last):
  File "a.py", line 2, in <module>
    np.zeros(2)
AttributeError: 'module' object has no attribute 'zeros'

如果我删除__init__.py ,它会工作。 __init__.py的唯一一行是对__all__的赋值。

为什么会这样呢?

您很可能有一个名为``numpy''的文件夹,该文件夹that is masking the library numpy`。 您不应命名文件/文件夹,以免它们掩盖库模块。

我的猜测是__init__.py位于名为numpy的文件夹内,当您对其进行裁剪时,Python停止将其识别为包,因此已加载了正确的numpy模块。

暂无
暂无

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

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