VSCODE - ModuleNotFoundError:没有名为“openpyxl”的模块

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

我正在研究 VSCode,我有 python 3.11.1(唯一的)和 openpyxl 3.1.2 使用 pip install openpyxl 并且在虚拟环境中安装成功(我已经验证过),但是当我想导入时使用这行代码的数据框:

df.to_excel('data.xlsx')
我得到这个错误

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'
python dataframe visual-studio-code openpyxl
© www.soinside.com 2019 - 2024. All rights reserved.