gpt4 book ai didi

python - Python 2.7 的 xlrd 导入问题

转载 作者:太空狗 更新时间:2023-10-29 17:24:29 25 4
gpt4 key购买 nike

我有一项任务是用 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

我在安装时没有收到任何错误消息。我还在/lib 文件夹的站点包中看到 xlrd-0.8.0 文件夹...

但是当我尝试导入它时,Python 无法识别它...

>>> import xlrd
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import xlrd
ImportError: No module named xlrd

您能建议如何找到问题吗?

最佳答案

如何重现和修复此错误:

打开python解释器,尝试导入xlrt,报错:

python
>>> import xlrt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named xlrt

<强>1。安装,或确保安装了 pip:

What is the official "preferred" way to install pip and virtualenv systemwide?

<强>2。安装 xlrd

pip install xlrd

提示:如果你觉得你必须使用 sudo pip install .... 来让它工作,那么你需要停下来了解为什么这是危险的。请参阅:What are the risks of running 'sudo pip'?

解决方法是使用特定用户安装 pip:pip install --user myuser ... 在这里使用您自己的最佳判断。确保你的 pip 在其中运行的目录归试图在那里安装软件包的用户所有。使用:chown -R $USER/Library/Python/2.7/site-packages

<强>3。在 python 解释器上测试它:

python
>>> import xlrd

>>> type(xlrd)
<type 'module'>
>>>

现在导入它没有问题,xlrd 是一个 python 模块。

故障排除:

如果你的 PYTHONPATH 没有定义,你应该定义它:

PYTHONPATH=:/home/el/wherever/where_to_find_modules

关于python - Python 2.7 的 xlrd 导入问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19407469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com