xlrd import issue with Python 2.7

孤者浪人 提交于 2019-11-30 12:13:12

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

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.

Max

For me, running python in spyder on a mac, it didn't work even after I installed xlrd using pip, because it was installed to a different location than the one spyder was using. To fix this, I first found where xlrd was installed to:

$pip install xlrd
Requirement already satisfied: xlrd in /usr/local/lib/python2.7/site-packages

Then copied the xlrd folder from there into where Spyder could access it:

$cd /Applications/Spyder.app/Contents/Resources/lib/python2.7/
$cp -r /usr/local/lib/python2.7/site-packages/xlrd.

Then updated the module within spyder, but I'm not sure whether this was necessary. Restarting Spyder might have also worked after making those changes.

If you're using conda,

conda install xlrd

Please add "C:\Python27\Lib\site-packages\" to your PYTHONPATH in your system variables.

If there is no such SYSTEM VARIABLE, please create it:

  1. Right-click the My Computer icon on your desktop and select Properties.
  2. Click the Advanced tab, then click the Environment Variables button.
  3. Under System Variables, click New.
  4. Enter the variable name as PYTHONPATH.
  5. Enter the variable value as C:\Python27\Lib\site-packages\
  6. Click OK.
  7. Click Apply Changes.

I had the same problem, seems like the is better if you export your xlsx to a csv file and then run the following on python

df = pd.read_csv('FileName.csv')

It should work like that. If you're using iPython or even better Jupyter then run df.head() to check if pandas reads your table properly.

Note, I am using Ubuntu

python -m pip install xlrd

For my case I have both python2 and python3 installed, pip install xlrd default install xlrd into the Python3 library (/usr/local/lib/python3.6/site-packages/xlrd/. By specifying python2 for pip install solved my problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!