码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 报错解决:pandas的依赖项openpyxl


    问题:

    使用pandas包,没有使用openpyxl包,但是报错:ImportError: Missing optional dependency 'openpyxl'.  Use pip or conda to install openpyxl.

    翻译:缺少可选择的依赖项“openpyxl”,使用 pip install openpyxl or conda install openpyxl

    解决方法:

    • 首先,激活你的项目环境:activate “name of your project”
    • 然后,安装openpyxl包:pip install openpyxl
    • 注:直接安装到conda环境下:conda install openpyxl

    此时,已经解决了遇到的问题,但是为什么呢,我们一起来分析pandas中的部分涉及openpyxl的文件的源码:


    (1)pandas中的_openpyxl.py文件

    类 OpenpyxlReader,说明了 pandas 在读取 xls 文件时,使用 openpyxl 引擎的阅读器(Reader using openpyxl engine)。

    (2)pandas中的_optional.py文件

    导入一个可以选择的依赖项。默认情况下,如果依赖项丢失,将引发带有好消息的 ImportError。 如果存在依赖项,但太旧,我们会提出。

    1. def import_optional_dependency(
    2. name: str,
    3. extra: str = "",
    4. errors: str = "raise",
    5. min_version: str | None = None,
    6. ):
    7. """
    8. Import an optional dependency.
    9. By default, if a dependency is missing an ImportError with a nice
    10. message will be raised. If a dependency is present, but too old,
    11. we raise.
    12. Parameters
    13. ----------
    14. name : str
    15. The module name.
    16. extra : str
    17. Additional text to include in the ImportError message.
    18. errors : str {'raise', 'warn', 'ignore'}
    19. What to do when a dependency is not found or its version is too old.
    20. * raise : Raise an ImportError
    21. * warn : Only applicable when a module's version is to old.
    22. Warns that the version is too old and returns None
    23. * ignore: If the module is not installed, return None, otherwise,
    24. return the module, even if the version is too old.
    25. It's expected that users validate the version locally when
    26. using ``errors="ignore"`` (see. ``io/html.py``)
    27. min_version : str, default None
    28. Specify a minimum version that is different from the global pandas
    29. minimum version required.
    30. Returns
    31. -------
    32. maybe_module : Optional[ModuleType]
    33. The imported module, when found and the version is correct.
    34. None is returned when the package is not found and `errors`
    35. is False, or when the package's version is too old and `errors`
    36. is ``'warn'``.
    37. """
    38. assert errors in {"warn", "raise", "ignore"}
    39. package_name = INSTALL_MAPPING.get(name)
    40. install_name = package_name if package_name is not None else name
    41. msg = (
    42. f"Missing optional dependency '{install_name}'. {extra} "
    43. f"Use pip or conda to install {install_name}."
    44. )
    45. try:
    46. module = importlib.import_module(name)
    47. except ImportError:
    48. if errors == "raise":
    49. raise ImportError(msg) from None
    50. else:
    51. return None
    52. # Handle submodules: if we have submodule, grab parent module from sys.modules
    53. parent = name.split(".")[0]
    54. if parent != name:
    55. install_name = parent
    56. module_to_get = sys.modules[install_name]
    57. else:
    58. module_to_get = module
    59. minimum_version = min_version if min_version is not None else VERSIONS.get(parent)
    60. if minimum_version:
    61. version = get_version(module_to_get)
    62. if Version(version) < Version(minimum_version):
    63. msg = (
    64. f"Pandas requires version '{minimum_version}' or newer of '{parent}' "
    65. f"(version '{version}' currently installed)."
    66. )
    67. if errors == "warn":
    68. warnings.warn(msg, UserWarning)
    69. return None
    70. elif errors == "raise":
    71. raise ImportError(msg)
    72. return module

    其中,报错的信息来自下面这四行代码,显示我们没有可选择的依赖项“openpyxl”,下载即可。

    1. msg = (
    2. f"Missing optional dependency '{install_name}'. {extra} "
    3. f"Use pip or conda to install {install_name}."
    4. )

    感悟:利用 python 对 Excal 表格进行增删查改对办公效率的提高有很大的帮助,本人习惯利用 pandas 和 xlswriter 对表格的读取、修改和保存。

    综合运用pandas和xlsxwriter解决所需问题(读取表格、更改数据、保存到新表格),附加一些注意事项_Flying Bulldog的博客-CSDN博客_pandas xlsxwriter(6)注意事项:pandas读取xlsx文件时,可能出现错误,这是需要另存为xls文件当worksheet.close()报错时,大概率不是此处的错误,可能是路径的问题写好内容格式后,一定要记得添加格式在字符串的指定位置插入内容,这个思想很重要,具体实现见上述代码https://blog.csdn.net/qq_54185421/article/details/124239391  >>>如有疑问,欢迎评论区一起探讨。

  • 相关阅读:
    Java杂学记录
    第一章:各种款式的算法复杂度例子+计算小技巧
    VUE+Spring Boot前后端分离开发实战(一):基于SpringBoot+Mybatis-plus+JWT+shiro+mysql后端登录接口实现
    java计算机毕业设计图书馆座位预约管理系统源代码+数据库+系统+lw文档
    纵横内外·突破盲区|BF-TR8500​高功率全频段数字中继台
    基于SpringBoot的服装生产管理系统的设计与实现
    基于springboot+vue.js+uniapp的房屋租赁系统附带文章源码部署视频讲解等
    UnitTest 参数化---Parameterized安装
    【Leetcode】469. Convex Polygon
    【排坑】websoucket场景下文件无法上传到服务器的解决方案
  • 原文地址:https://blog.csdn.net/qq_54185421/article/details/125444756
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号