简体   繁体   中英

Reading an Excel file and ImportError for xlrd in Jupyterlab

Similar questions must have been discussed a lot here, but they do not seem to be solved completely, so I want to update the discussion.

I started Python very recently and tried reading an Excel file in Jupyterlab (2.2.9) using

import pandas as pd
sample_file = pd.read_excel("sample.xlsx")
sample_file.head()

Then I got an error described below.

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-8-cfbece5b87d5> in <module>
----> 1 sample_data = pd.read_excel("sample_data.xlsx")
      2 sample_data.head()

~/.pyenv/versions/3.8.2/lib/python3.8/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(

~/.pyenv/versions/3.8.2/lib/python3.8/site-packages/pandas/io/excel/_base.py in __init__(self, io, engine)
    822         self._io = stringify_path(io)
    823 
--> 824         self._reader = self._engines[engine](self._io)
    825 
    826     def __fspath__(self):

~/.pyenv/versions/3.8.2/lib/python3.8/site-packages/pandas/io/excel/_xlrd.py in __init__(self, filepath_or_buffer)
     18         """
     19         err_msg = "Install xlrd >= 1.0.0 for Excel support"
---> 20         import_optional_dependency("xlrd", extra=err_msg)
     21         super().__init__(filepath_or_buffer)
     22 

~/.pyenv/versions/3.8.2/lib/python3.8/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 'xlrd'. Install xlrd >= 1.0.0 for Excel support Use pip or conda to install xlrd.

I use Anaconda and the version of xlrd was already 1.2.0. Just in case I already did

conda install xlrd

as advised by many people here (I tried replacing conda with pip as well). However, the same error still occurs. I am totally lost.

import pandas as pd
sample_file = pd.read_excel("sample.xlsx")
sample_file.head()

try this

The suggestion from pandas is a little out of date, I'm afraid. You need to:

  1. conda install openpyxl
  2. Restart Jupyterlab.
  3. Change your pandas code to be pd.read_excel("sample.xlsx", engine='openpyxl')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM