报错解决: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。 如果存在依赖项,但太旧,我们会提出。

def import_optional_dependency(
    name: str,
    extra: str = "",
    errors: str = "raise",
    min_version: str | None = None,
):
    """
    Import an optional dependency.

    By default, if a dependency is missing an ImportError with a nice
    message will be raised. If a dependency is present, but too old,
    we raise.

    Parameters
    ----------
    name : str
        The module name.
    extra : str
        Additional text to include in the ImportError message.
    errors : str {raise, warn, ignore}
        What to do when a dependency is not found or its version is too old.

        * raise : Raise an ImportError
        * warn : Only applicable when a modules version is to old.
          Warns that the version is too old and returns None
        * ignore: If the module is not installed, return None, otherwise,
          return the module, even if the version is too old.
          Its expected that users validate the version locally when
          using ``errors="ignore"`` (see. ``io/html.py``)
    min_version : str, default None
        Specify a minimum version that is different from the global pandas
        minimum version required.
    Returns
    -------
    maybe_module : Optional[ModuleType]
        The imported module, when found and the version is correct.
        None is returned when the package is not found and `errors`
        is False, or when the packages version is too old and `errors`
        is ``warn``.
    """

    assert errors in {"warn", "raise", "ignore"}

    package_name = INSTALL_MAPPING.get(name)
    install_name = package_name if package_name is not None else name

    msg = (
        f"Missing optional dependency {install_name}. {extra} "
        f"Use pip or conda to install {install_name}."
    )
    try:
        module = importlib.import_module(name)
    except ImportError:
        if errors == "raise":
            raise ImportError(msg) from None
        else:
            return None

    # Handle submodules: if we have submodule, grab parent module from sys.modules
    parent = name.split(".")[0]
    if parent != name:
        install_name = parent
        module_to_get = sys.modules[install_name]
    else:
        module_to_get = module
    minimum_version = min_version if min_version is not None else VERSIONS.get(parent)
    if minimum_version:
        version = get_version(module_to_get)
        if Version(version) < Version(minimum_version):
            msg = (
                f"Pandas requires version {minimum_version} or newer of {parent} "
                f"(version {version} currently installed)."
            )
            if errors == "warn":
                warnings.warn(msg, UserWarning)
                return None
            elif errors == "raise":
                raise ImportError(msg)

    return module

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

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

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

>>>如有疑问,欢迎评论区一起探讨。

经验分享 程序员 微信小程序 职场和发展