码农知识堂 - 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  >>>如有疑问,欢迎评论区一起探讨。

  • 相关阅读:
    【饭谈-鸡血篇】自以为对,就是错。
    Prometheus 监控系统
    【等保资料】等级保护定级指南及网络安全解读(ppt原件)
    【Vue项目复习笔记】商品评论信息的展示
    EN 13950隔热/隔音合成石膏灰泥墙板—CE认证
    架构设计杂谈
    Node.js事件循环
    Linux安装所需软件
    【免费赠送源码】Springboot旅游信息采集管理与分享系统n2qez计算机毕业设计-课程设计-期末作业-毕设程序代做
    【Android】设置全局标题栏
  • 原文地址:https://blog.csdn.net/qq_54185421/article/details/125444756
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号