You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VSCode中已安装openpyxl仍报ModuleNotFoundError求助

问题分析与解决

核心问题

从报错栈能看到,代码正在调用系统Python环境(c:\Users\kenneth\AppData\Local\Programs\Python\Python311\Lib\site-packages)里的pandas,但openpyxl是安装在虚拟环境(D:\Udemy\BD_Python\env\Lib\site-packages)中的,两者环境不匹配导致模块找不到。

解决步骤

  • 切换VSCode的Python解释器到虚拟环境
    点击VSCode左下角状态栏显示的Python版本,在弹出列表中选择虚拟环境目录下的Python可执行文件,路径应为D:\Udemy\BD_Python\env\Scripts\python.exe。

  • 确保终端激活虚拟环境
    关闭当前终端后重新打开,确认终端前缀显示(env);如果没有,手动执行激活命令:

    D:\Udemy\BD_Python\env\Scripts\activate
    
  • 验证当前解释器路径
    在代码开头添加以下代码,运行后检查输出是否为虚拟环境的Python路径:

    import sys
    print(sys.executable)
    # 正确输出应为 D:\Udemy\BD_Python\env\Scripts\python.exe
    
  • 重新同步环境依赖(可选)
    若以上步骤无效,激活虚拟环境后,先卸载再重新安装pandas和openpyxl:

    pip uninstall pandas openpyxl -y
    pip install pandas openpyxl
    

报错信息

ModuleNotFoundError                       Traceback (most recent call last)
Cell In[13], line 1
----> 1 df.to_excel('data.xlsx')

File c:\Users\kenneth\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\util\_decorators.py:211, in deprecate_kwarg.._deprecate_kwarg..wrapper(*args, **kwargs)
    209     else:
    210         kwargs[new_arg_name] = new_arg_value
--> 211 return func(*args, **kwargs)

File c:\Users\kenneth\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\util\_decorators.py:211, in deprecate_kwarg.._deprecate_kwarg..wrapper(*args, **kwargs)
    209     else:
    210         kwargs[new_arg_name] = new_arg_value
--> 211 return func(*args, **kwargs)

File c:\Users\kenneth\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\generic.py:2374, in NDFrame.to_excel(self, excel_writer, sheet_name, na_rep, float_format, columns, header, index, index_label, startrow, startcol, engine, merge_cells, encoding, inf_rep, verbose, freeze_panes, storage_options)
   2361 from pandas.io.formats.excel import ExcelFormatter
   2363 formatter = ExcelFormatter(
   2364     df,
   2365     na_rep=na_rep,
   (...)
   2372     inf_rep=inf_rep,
   2373 )
-> 2374 formatter.write(
   2375     excel_writer,
   2376     sheet_name=sheet_name,
   2377     startrow=startrow,
   2378     startcol=startcol,
   2379     freeze_panes=freeze_panes,
   2380     engine=engine,
   2381     storage_options=storage_options,
   2382 )

File c:\Users\kenneth\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\formats\excel.py:944, in ExcelFormatter.write(self, writer, sheet_name, startrow, startcol, freeze_panes, engine, storage_options)
    940     need_save = False
    941 else:
    942     # error: Cannot instantiate abstract class 'ExcelWriter' with abstract
    943     # attributes 'engine', 'save', 'supported_extensions' and 'write_cells'
--> 944     writer = ExcelWriter(  # type: ignore[abstract]
    945         writer, engine=engine, storage_options=storage_options
    946     )
    947     need_save = True
    949 try:

File c:\Users\kenneth\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\excel\_openpyxl.py:56, in OpenpyxlWriter.__init__(self, path, engine, date_format, datetime_format, mode, storage_options, if_sheet_exists, engine_kwargs, **kwargs)
     43 def __init__(
     44     self,
     45     path: FilePath | WriteExcelBuffer | ExcelWriter,
   (...)
     54 ) -> None:
     55     # Use the openpyxl module as the Excel writer.
---> 56     from openpyxl.workbook import Workbook
     58     engine_kwargs = combine_kwargs(engine_kwargs, kwargs)
     60     super().__init__(
     61         path,
     62         mode=mode,
   (...)
     65         engine_kwargs=engine_kwargs,
     66     )

ModuleNotFoundError: No module named 'openpyxl'

pip安装验证信息

(env) D:\Udemy\BD_Python>pip show openpyxl  
Name: openpyxl 
Version:3.1.2 
Summary: A Python library to read/write Excel 2010 xlsx/xlsm files 
Home-page: https://openpyxl.readthedocs.io 
Author: See AUTHORS 
Author-email: charlie.clark@clark-consulting.eu 
License: MIT 
Location: D:\Udemy\BD_Python\env\Lib\site-packages 
Requires: et-xm

内容的提问来源于stack exchange,提问作者Kenneth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 18:34:59