简体   繁体   中英

Python: “ImportError: no module named xlrd” after installing xlrd using pip

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).

you can try this:

import pip
package_name='xlrd'
pip.main(['install', package_name])

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)' 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.

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.

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

pip list

pip install xlrd


pip install xlrd == 1.2.0

now go to your IDE and start typing

import xlrd

its done now!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM