Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xlrd import issue with Python 2.7

I have an assignment to read excel data in Python. I have Python 2.7 installed. I tried installing xlrd0.8.0 with the following commands in Windows.

C:\Python27\xlrd-0.8.0>python setup.py build
running build
running build_py
creating build
creating build\lib
creating build\lib\xlrd
copying xlrd\biffh.py -> build\lib\xlrd
....


C:\Python27\xlrd-0.8.0>python setup.py install
running install
running build
running build_py
running build_scripts
running install_lib
running install_scripts
running install_egg_info
Writing C:\Python27\Lib\site-packages\xlrd-0.8.0-py2.7.egg-info

I don't get any error message while installing. I also see xlrd-0.8.0 folder in site-packages in /lib folder...

But when I try to import it, Python is not able to recognize it...

>>> import xlrd
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import xlrd
ImportError: No module named xlrd

Can you suggest how to find the issue?

like image 366
user2607677 Avatar asked Oct 16 '13 15:10

user2607677


People also ask

How do I add xlrd to python?

Type “ pip install xlrd ” (without quotes) in the command line and hit Enter again. This installs xlrd for your default Python installation. The previous command may not work if you have both Python versions 2 and 3 on your computer. In this case, try "pip3 install xlrd" or “ python -m pip install xlrd “.

What file types does xlrd support?

xlrd is a library for reading data and formatting information from Excel files in the historical . xls format. This library will no longer read anything other than . xls files.

What is xlrd module in python?

xlrd is a library for reading data and formatting information from Excel files in the historical . xls format.


2 Answers

How to reproduce and fix this error:

Open the python interpreter, try to import xlrt, you get an error:

python
>>> import xlrt
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named xlrt

1. Install, or make sure pip is installed:

What is the official "preferred" way to install pip and virtualenv systemwide?

2. Install xlrd

pip install xlrd

Protip: If you feel you have to use sudo pip install .... to get this to work then you need to stop and learn why this is dangerous. See: What are the risks of running 'sudo pip'?

Workarounds are to install pip using a certain user: pip install --user myuser ... use your own best judgment here. Make sure the directory your pip is operating in is owned by the user trying to install packages there. Use: chown -R $USER /Library/Python/2.7/site-packages.

3. Test it on the python interpreter:

python
>>> import xlrd

>>> type(xlrd)
<type 'module'>
>>>

Now it is imported it without a problem, xlrd is a python module.

Troubleshooting:

If your PYTHONPATH is not defined, you should define it:

PYTHONPATH=:/home/el/wherever/where_to_find_modules
like image 74
Eric Leschinski Avatar answered Sep 18 '22 19:09

Eric Leschinski


Resolving issue with xlrd import in Python 2.7

open this link https://bootstrap.pypa.io/get-pip.py and save as get-pip.py and copy this file into C:\Python2.7\

C:\Python2.7\python.exe get-pip.py

After this pip is installed in your system now installing xlrd

C:\Python2.7\python.exe -m pip install xlrd

Open python and import xlrd

import xlrd

it will work.

like image 42
Pavan Panchal Avatar answered Sep 22 '22 19:09

Pavan Panchal