0

After installing xlrd using pip, I am still unable to get it to work in my script. I'm not sure if I am missing something completely obvious - please help!

Currently, entering pip list on command line gives the following:

pip (9.0.1)
setuptools (32.1.0)
wheel (0.29.0)
xlrd (1.0.0)

And entering pip show xlrd gives:

Name: xlrd
Version: 1.0.0
Summary: Library for developers to extract data from Microsoft Excel (tm) spreadsheet files
Home-page: http://www.python-excel.org/
Author: John Machin
Author-email: sjmachin@lexicon.net
License: BSD
Location: /usr/local/lib/python2.7/site-packages
Requires:

Yet, if I go into the python environment (via typing python on command line) and try to import, here's what I get:

Python 2.7.10 (default, Feb  7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xlrd
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named xlrd

Anyone has encountered this problem before? I also have python 3 installed, and I downloaded xlrd via pip3. Yet import xlrd doesn't work on either versions (2 or 3).

Anthony Chen
  • 3
  • 1
  • 1
  • 2

5 Answers5

2

you can try this:

import pip
package_name='xlrd'
pip.main(['install', package_name])
Bibek Ghimire
  • 522
  • 1
  • 4
  • 19
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Donald Duck May 25 '17 at 19:25
1

First, you can check if such package actually exists on given directory (simply ls /usr/local/lib/python2.7/site-packages to see if there is __init__.py there).

If package is on place, you may check environmental variable $PYTHONPATH or just python -c 'import sys; print(sys.path)', which prints all directories where Python takes libraries from.

If everything is in place and sys.path contains site-packages (if that's not your first package installed via PIP I assume that everything's correct there), it might be problem with permissions. Please check some other stackoverflow question:

Cannot import a python module that is definitely installed (mechanize)

If package was installed with root's rw permission only - that would be the case.

erhesto
  • 1,176
  • 7
  • 20
  • 1
    Turns out my python environment didn't have `/usr/local/lib/python2.7/site-packages` as one of its sys.path. For me the situation is a bit trickier since I need different PYTHONPATH for different versions of python, bur for now `sys.path.append('/usr/local/lib/python2.7/site-packages')` works as a quick fix. Thanks! :) – Anthony Chen May 30 '17 at 12:37
  • @AnthonyChen If you use virtualenv (that I highly encourage to use), you can set different PYTHONPATH for each virtualenv, as stated here: https://stackoverflow.com/questions/4757178/how-do-you-set-your-pythonpath-in-an-already-created-virtualenv BTW if this fixes your problem you can mark it as proper solution, so other developers who search for same thing will find the answer. – erhesto May 30 '17 at 13:11
0

Uninstall and install xlrd. May be at installing time you would have got some error. This error shows only if xlrd is not installed. Use sudo pip install xlrd to install in python 2.7

Kondiba
  • 93
  • 6
  • Would you know if pip automatically appends paths to python's sys.path? If so perhaps that was what failed during installation. After checking sys.path following the other comment's suggestion I found that my python environment does not contain a path to `/usr/local/lib/python2.7/site-packages`, which seems rather unusual to me. – Anthony Chen May 30 '17 at 12:46
0

I solved the issue with upgrading pip and following Bibek Ghimire's answer. It seems that I was using pip version 9 whereas version 18 has been already realized.

Alla Sorokina
  • 563
  • 4
  • 8
0
pip list

pip install xlrd


pip install xlrd == 1.2.0

now go to your IDE and start typing

import xlrd

its done now!

Anil kumar
  • 19
  • 5