xlrd import issue with Python 2.7

user2607677 picture user2607677 · Oct 16, 2013 · Viewed 57.1k times · Source

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?

Answer

Eric Leschinski picture Eric Leschinski · Jan 5, 2015

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