Python 无法从模块导入 numpy?

Python numpy can't be imported from a module?

我的程序是:

import numpy as np
np.zeros(2)

它位于 __init__.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__.

的赋值

为什么会这样?

您很可能有一个名为 ``numpythat is masking the librarynumpy` 的文件夹。你不应该命名你的 files/folders 这样它们会掩盖库模块。

我的猜测是 __init__.py 位于名为 numpy 的文件夹中,当您删除它时,Python 不再将其识别为包,因此正确的 numpy 模块已加载。