Python 导入错误:没有名为“xlrd”的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45244238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:51:15  来源:igfitidea点击:

ImportError: No module named 'xlrd'

pythonpython-3.xpandasubuntupycharm

提问by Java

I am currently using PyCharm with Python version 3.4.3 for this particular project.

我目前正在将 PyCharm 与 Python 3.4.3 版用于此特定项目。

This PyCharm previously had Python2.7, and I upgraded to 3.4.3.

这个PyCharm之前有Python2.7,我升级到了3.4.3。

I am trying to fetch data from an Excel file using Pandas.

我正在尝试使用 Pandas 从 Excel 文件中获取数据。

Here is my code:

这是我的代码:

import pandas as pd

df = pd.read_excel("File.xls", "Sheet1")
print (df)

When I ran this code, I am getting this error.

当我运行此代码时,出现此错误。

ImportError: No module named 'xlrd'

I searched Stackoverflow and found some suggestions: I tried with

我搜索了 Stackoverflow 并找到了一些建议:我尝试过

pip install xlrd

But, when I did that, the message says

但是,当我这样做时,消息说

"Requirement already satisfied: xlrd in ./anaconda2/usr/lib/python2.7/site-packages"

Any suggestion?

有什么建议吗?

回答by aztec242

I had the same problem. I went to the terminal (Using Linux), and typed

我有同样的问题。我去了终端(使用 Linux),然后输入

sudo pip3 install xlrd

Then I imported xlrd in python and used the same code:

然后我在 python 中导入了 xlrd 并使用了相同的代码:

df = pd.read_excel("File.xlsx", "Sheet1")
print (df)

It worked for me!!

它对我有用!!

回答by Amal Raj

Running the pip install xlrdcompleted the installation, but that did not resolve the "no named module named xlrd" error.

运行pip install xlrd完成的安装,但这并没有解决“没有名为 xlrd 的命名模块”错误。

Copying the xlrd folder to the same folder where the .py programs are stored, resolved this issue.

将 xlrd 文件夹复制到存储 .py 程序的同一文件夹,解决了这个问题。

回答by Pedro Machado

You have to download xlrd library because pandas require it.

您必须下载 xlrd 库,因为大熊猫需要它。

In Pycharm I downloaded it in File -> Settings -> Project: [PROJECT NAME] -> Project Interpreter enter image description here

在 Pycharm 中,我在 File -> Settings -> Project: [PROJECT NAME] -> Project Interpreter 中下载了它 在此处输入图片说明

回答by Azhar Uddin

Click on the Bulb icon right next to the "import xlrd" & click on install package clrd , it will automatically install the package

单击“import xlrd”旁边的灯泡图标并单击安装包 clrd ,它将自动安装包

回答by Aklank Jain

The problem seems to be because of multiple python versions in the system, where requirement might be satisfied for one and not for the other.

问题似乎是因为系统中有多个 python 版本,其中一个可能满足而不是另一个。

In this case the requirement is satisfied for python2 but not for python3, you need to specify that the download needs to be for python3.

在这种情况下,python2 满足要求但 python3 不满足,您需要指定下载需要为 python3。

In reference to the answers mentioned above, what worked for me is

关于上面提到的答案,对我有用的是

python3 -m pip install xlrd

specifying python3 rather than pip3 worked for me.

指定 python3 而不是 pip3 对我有用。

回答by Adarsh Punj

For me the solution was uninstalling xlrdusing pip uninstall xlrd, and then installing it again using pip install xlrd.

对我来说,解决方案是xlrd使用卸载pip uninstall xlrd,然后使用pip install xlrd.

回答by Manu

If you are using a virtual environment use "pipenv install xlrd" instead of "pip install xlrd". That should work.

如果您使用的是虚拟环境,请使用“pipenv install xlrd”而不是“pip install xlrd”。那应该工作。

回答by Acsor

If you are in a terminal under Bash or any other semi-advanced shell with tab-completion, try to write pipfollowed by <tab>. If I do it, I see written:

如果您在 Bash 或任何其他具有制表符完成功能的半高级 shell 下的终端中,请尝试编写pip后跟<tab>. 如果我这样做,我会看到这样写:

none@vacuum:~$ pip  
pip     pip3    pip3.5  pip3.6

As you can see, I can choose to run pip commands under piponly, but I can choose even newer versions of pip. To know what version is associated to the pipcommand (with nothing else) run as usual pipwith the --versionor -Vflag. In my case, pip -Vyields:

如您所见,我可以选择pip仅在 pip 命令下运行,但我可以选择更新版本的 pip。要了解与pip命令关联的版本(没有其他任何内容),请像往常一样pip使用--versionor-V标志运行。在我的情况下,pip -V产量:

none@vacuum:~$ pip -V  
pip 9.0.1 from /usr/local/lib/python3.6/dist-packages (python 3.6)

Besides this, if you are developing under PyCharm, you may press Alt+Enterwhen the cursor is under the module name which cannot be imported to open a context-sensitive floating menu that will allow you to install the module. (You can also manage the list of installed modules for a specific Python version in the settings menu of PyCharm, under the Project Interpretersub-menu.)

除此之外,如果您是在PyCharm下开发,Alt+Enter当光标在无法导入的模块名称下时,您可以按下,打开上下文相关的浮动菜单,允许您安装模块。(您还可以在 PyCharm 的设置菜单中的Project Interpreter子菜单下管理特定 Python 版本的已安装模块列表。)

回答by David

I have python 2.7, 3.5 and 3.6 in my linux Mint machine for some reasons.

由于某些原因,我的 linux Mint 机器中有 python 2.7、3.5 和 3.6。

My spyder uses python 3.5 and I had the same issue. What I have done is

我的 spyder 使用 python 3.5,我遇到了同样的问题。我所做的是

  • go to the folder /usr/local/lib/python2.7/dist-packages
  • Copy the folder xlrd(Note that to do this action you have right click and open as root)
  • Now go to the /usr/local/lib/python3.5/dist-packagesor /usr/local/lib/python3.6/dist-packagesand paste the folder xlrdover there.
  • 转到文件夹 /usr/local/lib/python2.7/dist-packages
  • 复制文件夹xlrd(请注意,要执行此操作,您必须右键单击并以 root 身份打开)
  • 现在转到/usr/local/lib/python3.5/dist-packagesor/usr/local/lib/python3.6/dist-packages并将文件夹粘贴到xlrd那里。

It worked for me!!!

它对我有用!!!

This method does not change the default path so that, I can still continue with python 2.7 without any harm(something like SageMathwhich I use extensively)

此方法不会更改默认路径,因此我仍然可以继续使用 python 2.7 而不会造成任何伤害(类似于SageMath我广泛使用的东西)

回答by nat

The same happened to me using pycharm, I had installed it with pip, pip3 and anaconda and it still didn't work. I manually installed the package from pycharm-> preferences -> project -> project interpreter -> +and it worked.

我使用 pycharm 也发生了同样的情况,我已经用 pip、pip3 和 anaconda 安装了它,但它仍然无法正常工作。我手动安装了该软件包 pycharm-> preferences -> project -> project interpreter -> +并且它起作用了。