我在 Mac 上使用 Pandas 的 Anaconda 发行版。
我可以在 python 控制台中使用 Pandas 模块,但无法在脚本文件中使用它。 即,当我运行导入 pandas 模块的脚本时,出现属性错误。
例如,这个:
import pandas as pd
df = pd.DataFrame()
会给我这个错误:
Traceback (most recent call last):
File "/Path/To/File/file.py", line 1, in <module>
import pandas as pd
File "/Path/To/File/file.py", line 2, in <module>
df = pd.DataFrame()
AttributeError: 'module' object has no attribute 'DataFrame'
同样,这个:
import pandas as pd
df = pd.Series()
会给我这个错误:
Traceback (most recent call last):
File "/Path/To/File/file.py", line 1, in <module>
import pandas as pd
File "/Path/To/File/file.py", line 2, in <module>
sr = pd.Series()
AttributeError: 'module' object has no attribute 'Series'
然而,这两个在控制台上都可以正常工作。
我试过从 anaconda、pip 和 github 源卸载和重新安装,但无济于事。
谢谢。
你的脚本是pandas.py吗?
是的,是的。 (这现在有效,谢谢。)
澄清一下,这是我在另一个文件中遇到错误后创建的测试文件:
AttributeError: 'module' object has no attribute 'excel'
我现在意识到这是因为我放置原始脚本的目录还包含一个名为“csv.py”的文件,这显然在 Pandas 尝试加载内部模块时造成了严重破坏。
同样,我有一些具有 csv.py、csv1.py 等类似命名法的 spript。我将它们全部重命名,现在脚本在 ipython 上运行良好。
最有用的评论
你的脚本是pandas.py吗?