Import pandas module gives Attribute Error

2019-02-27 01:00发布

问题:

I recently changed from Windows to Ubuntu 12.04 LTS, and I am trying to get the hang on Python.

I copied a couple of example commands from http://pandas.pydata.org/pandas-docs/stable/10min.html into a .py file called pandas.py

This file was created in my home directory /home/myname

Here is an excerpt with the first 14 lines of /home/myname/pandas.py:

# pandas.py

###################################
## Testing out the pandas module ##
## Last update: Jan 2014         ##
## Author: me                    ##
###################################

import pandas as pd
import numpy as np

# Creating a Series by passing a list of values,
# letting pandas create a default integer index
s = pd.Series([1,3,5,np.nan,6,8])

when trying to run the script (using IDLE), I get the following message:

Traceback (most recent call last):
  File "/home/myname/mypandas.py", line 9, in <module>
    import pandas as pd
  File "/home/myname/pandas.py", line 14, in <module>
AttributeError: 'module' object has no attribute 'Series'

it appears as if Python tried to import my .py file rather that the actual module.

The same error appears if I write

import pandas

on my Python Shell. I changed the file name to mypandas.py, deleted the previous file, cleared the trash bin, and rebooted. This error just keeps appearing.

Other libraries such as numpy import just fine. I imagine there is some shell command which reverts this situation, but I am still not quite acquainted to Ubuntu. How can I fix this?

Thanks in advance

回答1:

Delete compiled script, pandas.pyc, from the location of your script, that is from your home directory. And don't name your scripts the same names as modules you're using.



回答2:

I've had similar error regarding the 'compat' attribute, restarting kernel helped