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

  • 相关阅读:
    Redis缓存的使用
    【测试】微软测试框架playwright的使用
    八位阿里p8耗时十年总结出Java面试复盘手册,带你实现逆风翻盘
    异常检测 | MATLAB实现BiLSTM(双向长短期记忆神经网络)数据异常检测
    shell编程基础(第10篇:字符串)
    DOCKER学习
    自己动手从零写桌面操作系统GrapeOS系列教程——1.1 GrapeOS介绍
    4. ceph存储使用流程
    Python代码优化工具——line_profile
    TechTool Pro for mac中文激活版:硬件监测和系统维护工具
  • 原文地址:https://blog.csdn.net/qq_54185421/article/details/125444756
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号