【问题标题】:openpyxl module does not have attribute '__version__' when imported by pandas熊猫导入时openpyxl模块没有属性'__version__'
【发布时间】:2016-08-30 14:26:36
【问题描述】:

我运行 pandas 的回溯将我带到:
site-packages\pandas\io\excel.py line 58, in get_writer AttributeError: 'module' object has no attribute '__version__'

我在 PyInstaller 存储库中找到了一个 git 问题的链接 https://github.com/pyinstaller/pyinstaller/issues/1890 找到了我的 openpyxl 版本,手动将其添加到 get_writer 方法中,如下所示:

def get_writer(engine_name):
    if engine_name == 'openpyxl':
        try:
            import openpyxl
            #remove when conda update available
            openpyxl.__version__ = '2.3.2'
            # with version-less openpyxl engine
            # make sure we make the intelligent choice for the user
            if LooseVersion(openpyxl.__version__) < '2.0.0':
                return _writers['openpyxl1']
            elif LooseVersion(openpyxl.__version__) < '2.2.0':
                return _writers['openpyxl20']
            else:
                return _writers['openpyxl22']
        except ImportError:
            # fall through to normal exception handling below
            pass

    try:
        return _writers[engine_name]
    except KeyError:
        raise ValueError("No Excel writer '%s'" % engine_name)

仍然没有骰子。错误回溯中给出的行号甚至没有改变。然后我将 openpyxl 版本更新为 2.3.5,仍然收到错误。 openpyxl init 文件中有一个 version 变量:

try:
    here = os.path.abspath(os.path.dirname(__file__))
    src_file = os.path.join(here, ".constants.json")
    with open(src_file) as src:
        constants = json.load(src)
        __author__ = constants['__author__']
        __author_email__ = constants["__author_email__"]
        __license__ = constants["__license__"]
        __maintainer_email__ = constants["__maintainer_email__"]
        __url__ = constants["__url__"]
        __version__ = constants["__version__"]
except IOError:
    # packaged
    pass

任何已知或潜在的修复或解决方法?

【问题讨论】:

  • 我不认为这与 Pandas 有任何关系,而是你如何安装 openpyxl。唯一受支持的安装方法是使用 pip。
  • @CharlieClark 我通过 pip 和 Anaconda 环境安装了它,两者都是 openpyxl 的 2.3.5 版本规范。我不知道关于 pip。
  • 如果 openpyxl 是使用 pip 安装的,那么包括版本在内的元数据将在包中公开。

标签: python pandas openpyxl conda


【解决方案1】:

编辑没有产生影响,因为进程被编译成这些模块正在运行的 exe。将我需要的部分导出到我的 anaconda 环境之外,现在该过程可以顺利进行。

【讨论】:

    【解决方案2】:

    我将在此讨论中添加我的解决方法,因为我在使用 openpyxl 2.4.0 时遇到了同样的问题,也许其他一些问题也被卡住了。 我发现要创建 .exe 文件,您必须恢复到旧版本的 openpyxl。这样做:

    1. 打开命令提示符并使用 'pip uninstall openpyxl' 卸载 openpyxl
    2. 使用旧版本 'pip install openpyxl==2.3.5' 重新安装 openpyxl

    现在您应该可以使用 py2exe、cx_freeze 等创建 .exe 文件了。

    【讨论】:

      【解决方案3】:

      这是我的解决方法:转到您的 openpyxl 站点包文件夹(对我来说是:C:\Python27\Lib\site-packages\openpyxl)。将 .constant.json 文件中的所有变量直接复制到 _ init _.py 文件中,如下所示:

      import json
      import os
      __author__= "See AUTHORS"
      __author_email__= "eric.gazoni@gmail.com"
      __license__= "MIT/Expat",
      __maintainer_email__= "openpyxl-users@googlegroups.com"
      __url__= "http://openpyxl.readthedocs.org"
      __version__= "2.4.0"
      try:
          here = os.path.abspath(os.path.dirname(__file__))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-29
        • 1970-01-01
        • 2021-09-26
        • 2017-03-26
        • 2021-08-25
        • 2018-10-22
        • 2021-03-09
        • 2021-10-02
        相关资源
        最近更新 更多