前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >『开发技巧』解决Python使用pandas读取xlsx文件报错“ImportError: Missing optional dependency ‘xlrd‘”的问题

『开发技巧』解决Python使用pandas读取xlsx文件报错“ImportError: Missing optional dependency ‘xlrd‘”的问题

作者头像
小宋是呢
发布2021-09-10 10:32:31
4.6K0
发布2021-09-10 10:32:31
举报
文章被收录于专栏:深度应用深度应用

0x01:引子

笔者在使用Mac进行Python开发时使用pandas读取xlsx文件遇到这个错误:

ImportError: Missing optional dependency 'xlrd'. Install xlrd >= 1.0.0 for Excel support Use pip or conda to install xlrd.

看似简单直接安装xlrd即可,实则在操作过程中并不顺利,又报出其他错误。笔者在这里分享一下自己遇到问题及解决步骤。

0x02:历程

按照提示“Use pip or conda to install xlrd.”,这里笔者使用pip安装,命令行指令如下:

代码语言:javascript
复制
pip install xlrd

输出为:可以看出,安装的为2.0.1版本xlrd,已满足xlrd >= 1.0.0的需求。

代码语言:javascript
复制
(ml) bash-3.2$ pip install xlrd
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting xlrd
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a6/0c/c2a72d51fe56e08a08acc85d13013558a2d793028ae7385448a6ccdfae64/xlrd-2.0.1-py2.py3-none-any.whl (96 kB)
     |████████████████████████████████| 96 kB 1.1 MB/s 
Installing collected packages: xlrd
Successfully installed xlrd-2.0.1

可是在执行代码时,还是报出以下错误:

代码语言:javascript
复制
(ml) bash-3.2$ python demo.py 
Traceback (most recent call last):
  File "demo.py", line 6, in <module>
    datas = pd.read_excel("data.xlsx")
  File "/Users/song/miniforge3/envs/ml/lib/python3.8/site-packages/pandas/util/_decorators.py", line 299, in wrapper
    return func(*args, **kwargs)
  File "/Users/song/miniforge3/envs/ml/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 336, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
  File "/Users/song/miniforge3/envs/ml/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 1080, in __init__
    raise ValueError(
ValueError: Your version of xlrd is 2.0.1. In xlrd >= 2.0, only the xls format is supported. Install openpyxl instead.

关键信息:Your version of xlrd is 2.0.1. In xlrd >= 2.0, only the xls format is supported. Install openpyxl instead.

xlrd版本问题,大于等于2.0时,仅支持xls格式,建议安装openpyxl。

其实有两种解决方法

  • 对xlrd进行降级用来支持xlsx
  • 安装openpyxl替代对xlrd依赖

笔者这里选择的是对xlrd降级操作,安装1.0版本xlrd,指令:pip install xlrd==1.0

提示:1.”==“符号用于指定版本,注意是双等于号,这里与conda单等于号不同。2.在使用pip降级安装时,不用手动卸载高版本,系统会直接替换。

代码语言:javascript
复制
(ml) bash-3.2$ pip install xlrd==1
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting xlrd==1
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/0c/b0/8946fe3f9c2690c164aaa88dfd43b56347d3cdeac34124b988acd1aaa151/xlrd-1.0.0-py3-none-any.whl (143 kB)
     |████████████████████████████████| 143 kB 150 kB/s 
Installing collected packages: xlrd
  Attempting uninstall: xlrd
    Found existing installation: xlrd 2.0.0
    Uninstalling xlrd-2.0.0:
      Successfully uninstalled xlrd-2.0.0
Successfully installed xlrd-1.0.0

笔者这里安装了1.0版本,不太幸运的是在运行又出错了~~,如下:

代码语言:javascript
复制
(ml) bash-3.2$ python demo.py 
Traceback (most recent call last):
  File "demo.py", line 6, in <module>
    datas = pd.read_excel("data.xlsx")
  File "/Users/song/miniforge3/envs/ml/lib/python3.8/site-packages/pandas/util/_decorators.py", line 299, in wrapper
    return func(*args, **kwargs)
  File "/Users/song/miniforge3/envs/ml/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 336, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
  File "/Users/song/miniforge3/envs/ml/lib/python3.8/site-packages/pandas/io/excel/_base.py", line 1052, in __init__
    xlrd_version = LooseVersion(xlrd.__version__)
AttributeError: module 'xlrd' has no attribute '__version__'

继续设法解决,笔者考虑,可能是1.0版本过旧的缘故。笔者试着安装了1.x版本。尝试几次后,安装了1.2:

代码语言:javascript
复制
(ml) bash-3.2$ pip install xlrd==1.8
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
ERROR: Could not find a version that satisfies the requirement xlrd==1.8
ERROR: No matching distribution found for xlrd==1.8
(ml) bash-3.2$ pip install xlrd==1.6
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
ERROR: Could not find a version that satisfies the requirement xlrd==1.6
ERROR: No matching distribution found for xlrd==1.6
(ml) bash-3.2$ pip install xlrd==1.2
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting xlrd==1.2
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/b0/16/63576a1a001752e34bf8ea62e367997530dc553b689356b9879339cf45a4/xlrd-1.2.0-py2.py3-none-any.whl (103 kB)
     |████████████████████████████████| 103 kB 61 kB/s 
Installing collected packages: xlrd
  Attempting uninstall: xlrd
    Found existing installation: xlrd 1.0.0
    Uninstalling xlrd-1.0.0:
      Successfully uninstalled xlrd-1.0.0
Successfully installed xlrd-1.2.0

此时读取就正常了。

0x03:后记

这个博客对你有用的话欢迎收藏转发,也麻烦可爱又爱学的你能赏个赞,菜小宋更博不易,在这里谢过啦。

如果你想学习更多开发技巧与AI算法,欢迎搜索关注笔者公众号“简明AI”,和爱学习讨论的小伙伴一起交流学习。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-04-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 0x01:引子
  • 0x02:历程
  • 0x03:后记
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档