Python错误集锦:AttributeError: partially initialized module ‘numpy’ has no attribute ‘ones’ (most likely due to a circular import)

原文链接:http://www.juzicode.com/archives/3930

错误提示:

调用numpy.ones()提示:AttributeError: partially initialized module ‘numpy’ has no attribute ‘ones’ (most likely due to a circular import)

#juzicode.com/vx:桔子code
import numpy
a = numpy.ones((5,5))
print(a)
==========运行结果:

E:\juzicode\code-py\test>python numpy.py
 Traceback (most recent call last):
   File "numpy.py", line 2, in 
     import numpy
   File "E:\juzicode\code-py\test\numpy.py", line 3, in 
     a = numpy.ones((5,5))
 AttributeError: partially initialized module 'numpy' has no attribute 'ones' (most likely due to a circular import)

错误原因:

1、要执行的文件名称为numpy.py,和安装的第3方模块numpy重名。

解决方法:

1、修改自己编写的py文件名称为其他文件名,比如改为numpytest.py,避免和导入模块冲突:

==========运行结果:
E:\juzicode\code-py\test>python numpytest.py
 [[1. 1. 1. 1. 1.]
  [1. 1. 1. 1. 1.]
  [1. 1. 1. 1. 1.]
  [1. 1. 1. 1. 1.]
  [1. 1. 1. 1. 1.]]

扩展内容:

  1. 好冷的Python~ 那些同名的家伙们(Python作用域)
  2. Python错误集锦:ModuleNotFoundError: No module named ‘matplotlib.pyplot’; ‘matplotlib’ is not a package 


如果本文还没有完全解决你的疑惑,你也可以在微信公众号“桔子code”后台给我留言,欢迎一起探讨交流。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注