虚拟环境中import pandas_datareader报ImportError: cannot import name 'is_list_like'
ImportError: cannot import name 'is_list_like' with pandas_datareader Hey there, let's get this error sorted out for you. The issue you're facing is a classic version mismatch between pandas and pandas_datareader:
What's causing the error?
In newer versions of pandas, the is_list_like utility function was moved from pandas.core.common to pandas.api.types. But the version of pandas_datareader installed in your virtual environment is still trying to import it from the old location, hence the ImportError.
Here are 3 ways to fix this:
Option 1: Upgrade pandas_datareader (Recommended)
This is the simplest fix—just updatepandas_datareaderto a version that's compatible with your pandas installation. Run this command in your virtual environment:pip install --upgrade pandas_datareaderAfter upgrading, try running your script again; the import should work without issues.
Option 2: Manually fix the import path in pandas_datareader
If upgrading isn't an option (e.g., your Python 3.5 is too old for the latest pandas_datareader), you can edit the problematic file directly:- Navigate to the
fred.pyfile at this path:/home/xxxxx/django-apps/env/lib/python3.5/site-packages/pandas_datareader/fred.py - Open it with a text editor, and replace the first line:
with:from pandas.core.common import is_list_likefrom pandas.api.types import is_list_like - Save the file and re-run your script.
- Navigate to the
Option 3: Downgrade pandas (Not Recommended)
As a last resort, you can roll back pandas to an older version whereis_list_likestill lives inpandas.core.common. Run:pip install pandas==0.23.4Note: This might cause compatibility issues with other parts of your code that rely on newer pandas features, so only use this if the first two options don't work.
Once you've tried one of these solutions, test your import code again—you should be able to import pandas_datareader without hitting the error.
内容的提问来源于stack exchange,提问作者Raj

