ImportError: No module named 'xlrd'

2020-02-11 18:09发布

问题:

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

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

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

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

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?

回答1:

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

sudo pip3 install xlrd

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

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

It worked for me!!



回答2:

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

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



回答3:

You have to download xlrd library because pandas require it.

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



回答4:

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



回答5:

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.

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.

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.



回答6:

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

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

As you can see, I can choose to run pip commands under pip only, but I can choose even newer versions of pip. To know what version is associated to the pip command (with nothing else) run as usual pip with the --version or -V flag. In my case, pip -V yields:

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+Enter when 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 Interpreter sub-menu.)



回答7:

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

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

  • 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-packages or /usr/local/lib/python3.6/dist-packages and paste the folder xlrd over there.

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 SageMath which I use extensively)



回答8:

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.



回答9:

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