ImportError: Missing optional dependency 'openpyxl'. Use pip or conda to install openpyxl.
Fixes ImportError: Missing optional dependency 'openpyxl'. Use pip or conda to install openpyxl. Use when pd.read_excel / ExcelFile / to_excel on .xlsx raises Missing optional dependency 'openpyxl' Not for: the file itself is missing (FileNotFoundError) or corrupt (BadZipFile)
How to fix ImportError: Missing optional dependency 'openpyxl'. Use pip or conda to install openpyxl.
Install the Excel engine: python -m pip install openpyxl. pandas deliberately does not bundle Excel readers; read_excel on an .xlsx file needs openpyxl installed in the same environment. Use the exact same python/pip pair (python -m pip) so it lands in the right env.
The exact error
ImportError: Missing optional dependency 'openpyxl'. Use pip or conda to install openpyxl.Fix it
- Run
python -m pip install openpyxl.
Expected: pip reports Successfully installed openpyxl
- Retry
pd.read_excel('file.xlsx').
Expected: returns a DataFrame, no ImportError
- For legacy .xls files you need xlrd instead:
python -m pip install xlrd.
Expected: read_excel works on .xls
When this applies
pd.readexcel / ExcelFile / toexcel on .xlsx raises Missing optional dependency 'openpyxl'
When this does NOT apply
the file itself is missing (FileNotFoundError) or corrupt (BadZipFile)
Versions
pandas >= 1.0 reading .xlsx
Why it happens
Excel IO engines are optional dependencies; pandas raises a helpful ImportError telling you which one to install.
Edge cases
xlrd >= 2.0 dropped .xlsx support, so do not 'fix' this by installing xlrd for an .xlsx file.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.