AttributeError:模块对象没有属性"Series".代码适用于iPython

10 python ipython python-import python-2.7 pandas

子模块不是隐式导入的,必须显式声明,但我正在对子模块进行显式调用pd.Series,不是吗?

无论如何,不​​应该被import pandas as pd允许pd.Series被召唤?以下代码在iPython中完美运行,但在从脚本执行时失败.

#!/usr/bin/env/python2.7
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np

counts = pd.Series([632, 1638, 569, 115], index=["Firmicutes", "Proteobacteria", "Actinobacteria", "Bacteroidetes"])
Run Code Online (Sandbox Code Playgroud)

结果是:

tyler@machine ~/src/stats $ python pandas.py
Traceback (most recent call last):
  File "pandas.py", line 3, in <module>
    import pandas as pd
  File "/home/tyler/src/stats/pandas.py", line 6, in <module>
    counts = pd.Series([632, 1638, 569, 115], index=["Firmicutes", "Proteobacteria", "Actinobacteria", "Bacteroidetes"])
AttributeError: 'module' object has no attribute 'Series'
Run Code Online (Sandbox Code Playgroud)

我哪里出错了?

Phi*_*oud 22

问题是你已经调用了你的模块pandas.称之为别的东西.并且不要忘记删除pandas.pyc生成的内容import pandas,否则它将继续失败.