Skip to content

BUG: ImportError: Missing optional dependency 'openpyxl'. Use pip or conda to install openpyxl. #41715

@ranabanik

Description

@ranabanik

I am running the following code from Jupyter notebook.

dataDir =  r'D:\\'
files = glob(os.path.join(dataDir, '*.xlsx'))
print(files)
if os.path.isfile(files[0]):
    print('ok')
df = pd.read_excel(files[0], engine='openpyxl')

which prints:
['D:\\\\file_index_all.xlsx', 'D:\\\\file_index_all2.xlsx']
ok

But I get the following error.

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-52-5dc0ef3ce47b> in <module>
      6 if os.path.isfile(files[0]):
      7     print('ok')
----> 8 df = pd.read_excel(files[0], engine='openpyxl')

c:\users\ranab\miniconda3\lib\site-packages\pandas\io\excel\_base.py in read_excel(io, sheet_name, header, names, index_col, usecols, squeeze, dtype, engine, converters, true_values, false_values, skiprows, nrows, na_values, keep_default_na, verbose, parse_dates, date_parser, thousands, comment, skipfooter, convert_float, mangle_dupe_cols, **kwds)
    302 
    303     if not isinstance(io, ExcelFile):
--> 304         io = ExcelFile(io, engine=engine)
    305     elif engine and engine != io.engine:
    306         raise ValueError(

c:\users\ranab\miniconda3\lib\site-packages\pandas\io\excel\_base.py in __init__(self, io, engine)
    819         self._io = stringify_path(io)
    820 
--> 821         self._reader = self._engines[engine](self._io)
    822 
    823     def __fspath__(self):

c:\users\ranab\miniconda3\lib\site-packages\pandas\io\excel\_openpyxl.py in __init__(self, filepath_or_buffer)
    482             Object to be parsed.
    483         """
--> 484         import_optional_dependency("openpyxl")
    485         super().__init__(filepath_or_buffer)
    486 

c:\users\ranab\miniconda3\lib\site-packages\pandas\compat\_optional.py in import_optional_dependency(name, extra, raise_on_missing, on_version)
     90     except ImportError:
     91         if raise_on_missing:
---> 92             raise ImportError(msg) from None
     93         else:
     94             return None

ImportError: Missing optional dependency 'openpyxl'.  Use pip or conda to install openpyxl.

pandas version:
pd.__version__
1.0.1
Also one other thing I noticed despite having openpyxl 3.0.7 I can't import it in jupyter notebook but in pycharm

import openpyxl
print(openpyxl.__version__)

shows 3.0.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds InfoClarification about behavior needed to assess issueNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions