无法使用 Anaconda Prompt 更新到最新的 OpenPyXL 版本 3.1 或更高版本

问题描述 投票:0回答:1

我正在尝试使用“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'.

关于如何协调这个问题有什么建议吗?

python version-control anaconda openpyxl
1个回答
0
投票

我遇到了类似的问题。我将分享复制您的问题的 pandas 的最小工作安装和解决方案。

使用自制软件安装 anaconda 示例:

举例说明:从自制软件安装 miniconda,使用 conda 将 anaconda 堆栈安装到名为 demo 的环境中,然后激活“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 频道,然后升级(请注意,这一切都在演示环境中:
conda activate demo
):

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

现在您可以确认

openpyxl
(
conda list openpyxl
) 的版本并确认
pd.read_excel()
可以工作。

两个注意事项:首先,如果您不更改通道,当您尝试通过默认通道升级时可能会出现错误,其次这似乎与 pandas GitHub 上提出的早期问题相关。

© www.soinside.com 2019 - 2024. All rights reserved.