Python 2.7 的 xlrd 导入问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19407469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
xlrd import issue with Python 2.7
提问by user2607677
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.
我有一个任务是用 Python 读取 excel 数据。我安装了 Python 2.7。我尝试在 Windows 中使用以下命令安装 xlrd0.8.0。
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...
安装时我没有收到任何错误消息。我还在 /lib 文件夹中的站点包中看到了 xlrd-0.8.0 文件夹...
But when I try to import it, Python is not able to recognize it...
但是当我尝试导入它时,Python 无法识别它......
>>> 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?
你能建议如何找到问题吗?
回答by Eric Leschinski
How to reproduce and fix this error:
如何重现和修复此错误:
Open the python interpreter, try to import xlrt, you get an error:
打开python解释器,尝试导入xlrt,报错:
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:
1. 安装,或者确保安装了pip:
What is the official "preferred" way to install pip and virtualenv systemwide?
在系统范围内安装 pip 和 virtualenv 的官方“首选”方式是什么?
2. Install xlrd
2.安装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'?
提示:如果你觉得你必须使用它sudo pip install ....
来让它工作,那么你需要停下来了解为什么这是危险的。请参阅:运行“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
.
解决方法是使用特定用户安装 pip:pip install --user myuser ...
在这里使用您自己的最佳判断。确保您的 pip 所在的目录由尝试在那里安装软件包的用户拥有。用途:chown -R $USER /Library/Python/2.7/site-packages
。
3. Test it on the python interpreter:
3.在python解释器上测试:
python
>>> import xlrd
>>> type(xlrd)
<type 'module'>
>>>
Now it is imported it without a problem, xlrd is a python module.
现在它被导入它没有问题,xlrd是一个python模块。
Troubleshooting:
故障排除:
If your PYTHONPATH is not defined, you should define it:
如果你的 PYTHONPATH 没有定义,你应该定义它:
PYTHONPATH=:/home/el/wherever/where_to_find_modules
回答by be_good_do_good
Please add "C:\Python27\Lib\site-packages\" to your PYTHONPATH in your system variables.
请将“C:\Python27\Lib\site-packages\”添加到系统变量中的 PYTHONPATH 中。
If there is no such SYSTEM VARIABLE, please create it:
如果没有这样的系统变量,请创建它:
- Right-click the My Computer icon on your desktop and select Properties.
- Click the Advanced tab, then click the Environment Variables button.
- Under System Variables, click New.
- Enter the variable name as PYTHONPATH.
- Enter the variable value as C:\Python27\Lib\site-packages\
- Click OK.
- Click Apply Changes.
- 右键单击桌面上的“我的电脑”图标,然后选择“属性”。
- 单击高级选项卡,然后单击环境变量按钮。
- 在系统变量下,单击新建。
- 输入变量名作为 PYTHONPATH。
- 输入变量值 C:\Python27\Lib\site-packages\
- 单击确定。
- 单击应用更改。
回答by Pavan Panchal
Resolving issue with xlrd import in Python 2.7
解决 Python 2.7 中 xlrd 导入的问题
open this link https://bootstrap.pypa.io/get-pip.pyand save as get-pip.py and copy this file into C:\Python2.7\
打开此链接https://bootstrap.pypa.io/get-pip.py并另存为 get-pip.py 并将此文件复制到 C:\Python2.7\
C:\Python2.7\python.exe get-pip.py
After this pip is installed in your system now installing xlrd
在您的系统中安装此 pip 后,现在安装 xlrd
C:\Python2.7\python.exe -m pip install xlrd
Open python and import xlrd
打开python并导入xlrd
import xlrd
it will work.
它会起作用。
回答by Max
For me, running python in spyderon a mac, it didn't work even after I installed xlrd
using pip, because it was installed to a different location than the one spyderwas using. To fix this, I first found where xlrd
was installed to:
对我来说,在Mac 上的spyder 中运行 python,即使在我xlrd
使用 pip安装后它也不起作用,因为它安装到的位置与spyder使用的位置不同。为了解决这个问题,我首先找到了xlrd
安装位置:
$pip install xlrd
Requirement already satisfied: xlrd in /usr/local/lib/python2.7/site-packages
Then copied the xlrd
folder from there into where Spydercould access it:
然后将xlrd
文件夹从那里复制到Spyder可以访问它的地方:
$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 Spydermight have also worked after making those changes.
然后在spyder 中更新了模块,但我不确定这是否有必要。进行这些更改后,重新启动Spyder可能也有效。
回答by cdlm
If you're using conda,
如果您使用的是 conda,
conda install xlrd
回答by Tasos Dalis
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
我遇到了同样的问题,如果您将 xlsx 导出到 csv 文件,然后在 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.
它应该像那样工作。如果您使用的是 iPython 或更好的 Jupyter df.head()
,请运行以检查 pandas 是否正确读取您的表。
Note, I am using Ubuntu
注意,我使用的是 Ubuntu
回答by Penny Qian
python -m pip install xlrd
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.
对于我的情况,我同时安装了 python2 和 python3,pip install xlrd
默认将 xlrd 安装到 Python3 库中(/usr/local/lib/python3.6/site-packages/xlrd/
。通过为 pip install 指定 python2 解决了我的问题。