导入 pandas 模块给出属性错误 [英] Import pandas module gives Attribute Error

查看:29
本文介绍了导入 pandas 模块给出属性错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从 Windows 更改为 Ubuntu 12.04 LTS,并且我正在尝试使用 Python.

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

我复制了几个示例命令http://pandas.pydata.org/pandas-docs/stable/10min.html进入一个名为 pandas.py 的 .py 文件

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

这个文件是在我的主目录/home/myname 中创建的

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

这是/home/myname/pandas.py 前 14 行的摘录:

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])

尝试运行脚本(使用 IDLE)时,我收到以下消息:

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'

看起来好像 Python 试图导入我的 .py 文件而不是实际模块.

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

在我的 Python Shell 上.我将文件名更改为 mypandas.py,删除了之前的文件,清除了垃圾箱,然后重新启动.这个错误一直出现.

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.

其他库如 numpy import 就好了.我想有一些 shell 命令可以恢复这种情况,但我仍然不太熟悉 Ubuntu.我该如何解决这个问题?

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?

提前致谢

推荐答案

从您的脚本所在的位置,即您的主目录中删除已编译的脚本,pandas.pyc.并且不要将您的脚本命名为与您正在使用的模块相同的名称.

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.

这篇关于导入 pandas 模块给出属性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆