无法通过Anaconda Prompt更新到最新的OpenPyXL 3.1或更高版本

1 投票
1 回答
220 浏览
提问于 2025-04-14 16:21

我正在尝试使用'openpyxl'库和pandas,但运行代码时遇到了以下错误。

ImportError                               Traceback (most recent call last)
Cell In[27], line 1
----> 1 message_data = (pd.read_excel('message_types.xlsx',
      2                               sheet_name='messages')
      3                 .sort_values('id')
      4                 .drop('id', axis=1))

File c:\PATH.py:495, in read_excel(io, sheet_name, header, names, index_col, usecols, dtype, engine, converters, true_values, false_values, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, parse_dates, date_parser, date_format, thousands, decimal, comment, skipfooter, storage_options, dtype_backend, engine_kwargs)
    493 if not isinstance(io, ExcelFile):
    494     should_close = True
--> 495     io = ExcelFile(
    496         io,
    497         storage_options=storage_options,
    498         engine=engine,
    499         engine_kwargs=engine_kwargs,
    500     )
    501 elif engine and engine != io.engine:
    502     raise ValueError(
    503         "Engine should not be specified when passing "
    504         "an ExcelFile - ExcelFile already has the engine set"
    505     )

File c:\PATH.py:1567, in ExcelFile.__init__(self, path_or_buffer, engine, storage_options, engine_kwargs)
   1564 self.engine = engine
...
--> 164     raise ImportError(msg)
    165 else:
    166     return None

ImportError: Pandas requires version '3.1.0' or newer of 'openpyxl' (version '3.0.10' currently installed).

我已经尝试更新'openpyxl'的版本,并确认'openpyxl'安装正确,但Anaconda导航器显示最新版本是'3.0.10',而'openpyxl'的文档中显示最新版本是'3.1.2'。

有没有什么建议可以解决这个问题?

1 个回答

1

我遇到过类似的问题。这里我会分享一个简单的pandas安装方法,这个方法能复现你遇到的问题,还有一个解决方案。

使用Homebrew安装Anaconda的示例:

举个例子:从Homebrew安装miniconda,然后用conda在一个叫做demo的环境里安装Anaconda的所有组件,接着激活'demo'环境。

brew install miniconda
conda create -n demo python=3.11 anaconda

进入这个环境并检查版本:

conda activate demo 
conda list pandas

会得到:

# packages in environment at /opt/homebrew/Caskroom/miniconda/base/envs/demo:
#
# Name                    Version                   Build  Channel
pandas                    2.1.4           py311h7aedaa7_0

还有

conda list openpyxl 

会得到:

# packages in environment at /opt/homebrew/Caskroom/miniconda/base/envs/demo:
#
# Name                    Version                   Build  Channel
openpyxl                  3.0.10          py311h80987f9_0

现在启动 ipython,并复现错误(使用一个之前加载过且没有错误的 .xlsx 文件)。

ipython 
import pandas as pd 
df = pd.read_xlsx("demo.xlsx")

会出现你报告的同样错误(前提是你的python路径里没有正确的 openpyxl,否则这个错误就不容易复现)。

解决方案:

解决方案是手动使用conda升级 openpyxl。为此,我需要告诉conda去查找condaforge这个频道,然后进行升级(注意这都是在demo环境里进行的: conda activate demo):

conda config --add channels conda-forge 
conda install openpyxl=3.1.0 

现在你可以确认 openpyxl 的版本(用 conda list openpyxl),并确认 pd.read_excel() 可以正常工作。

有两点需要注意:第一,如果你不更改频道,可能在通过默认频道升级时会遇到错误;第二,这似乎与之前在 pandas GitHub上提出的一个问题有关。

这个解决方案并不完美,因为理想情况下,只需要使用 conda create -n demo python=3.11 anaconda 就能得到一个正常工作的Anaconda安装。

撰写回答