简体   繁体   English

无法从模块导入Python numpy?

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

My program is just: 我的程序是:

import numpy as np
np.zeros(2)

It's in a folder with a __init__.py 它在带有__init__.py的文件夹中

If I run it as python a.py , I get: 如果我将其作为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'

If I remove __init__.py , it works. 如果我删除__init__.py ,它会工作。 The only line in __init__.py is an assignment to __all__ . __init__.py的唯一一行是对__all__的赋值。

Why is this happening? 为什么会这样呢?

You most probably have a folder with the name ``numpy that is masking the library numpy` . 您很可能有一个名为``numpy''的文件夹,该文件夹that is masking the library numpy`。 You should not name your files/folders such that they mask the library modules. 您不应命名文件/文件夹,以免它们掩盖库模块。

My guess is this __init__.py was inside the folder named numpy , when you rmeoved it, Python stopped recognizing it as a package, and hence the correct numpy module was loaded. 我的猜测是__init__.py位于名为numpy的文件夹内,当您对其进行裁剪时,Python停止将其识别为包,因此已加载了正确的numpy模块。

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

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