[解决方法]AttributeError: module ‘numpy‘ has no attribute ‘loadtxt‘

问题介绍

在pycharm中写了很普通的一段代码:

import numpy as np

with open('泰坦尼克号数据3.csv', encoding='utf-8') as f:
    array = np.loadtxt(f, str, delimiter=',', encoding='utf-8')
    array_title = array[0]

print(array)

在Jupyter中能完美运行,但在Pycharm中不能运行,会报错:

AttributeError: module 'numpy' has no attribute 'loadtxt'

问题分析

可以肯定的是,numpy是有loadtxt属性的,那这里为什么会报错呢?原来,我在Pycharm新建的py文件的文件名也叫numpy,和安装的库名冲突了。

解决方法

在Pycharm中把自己新建的py文件改个名字就行啦。

经验总结

以后像这种:AttributeError: module '(?????)' has no attribute '(?????)'类型的错误,都是由自己新建的py文件名和引用的库函数重名导致的。换个名字即可排除错误。

猜你喜欢

转载自blog.csdn.net/yuanchenglei/article/details/121325068
今日推荐