ImportError No module named pandas
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The error ImportError: No module named pandas (or ModuleNotFoundError: No module named 'pandas' in Python 3.6+) means Python cannot find the pandas library in its current environment. This is typically caused by pandas not being installed, being installed in a different Python environment, or a virtual environment not being activated.
Quick Fix
Install pandas in your current Python environment:
Or with a specific Python version:
If the error persists after installation, the issue is almost certainly an environment mismatch.
Common Causes and Solutions
1. Pandas Not Installed
The most straightforward case — pandas has not been installed:
2. Wrong Python Environment
The most common cause of confusion. Pandas is installed in one Python installation, but you are running a different one:
On systems with multiple Python versions (e.g., Python 3.8, 3.9, 3.10), pip might install to 3.8 while you run scripts with 3.10.
3. Virtual Environment Not Activated
If you installed pandas in a virtual environment, you must activate it:
4. Jupyter Notebook Using a Different Kernel
Jupyter may use a different Python kernel than your terminal:
Install pandas for the specific kernel:
5. IDE Using a Different Interpreter
IDEs like VS Code, PyCharm, or Spyder have their own Python interpreter settings:
VS Code:
- Open Command Palette (Ctrl+Shift+P)
- Search "Python: Select Interpreter"
- Choose the environment where pandas is installed
PyCharm:
- Go to Settings > Project > Python Interpreter
- Select the correct interpreter or add pandas via the "+" button
6. System Python vs User Python
On Linux/macOS, using sudo pip install installs to the system Python, while your user may run a different Python:
Alternative Installation Methods
Using conda (Anaconda/Miniconda)
With specific version
From requirements.txt
Where requirements.txt contains:
Verifying the Installation
Check the installation path matches your Python environment:
Common Pitfalls
- Naming conflicts: Do not name your own script
pandas.py— Python will import your file instead of the library. Also check for apandas/directory in your project. - Mixing pip and conda: If you use Anaconda, prefer
conda install pandasoverpip install pandas. Mixing package managers can cause dependency conflicts. - Permissions on shared servers: On shared servers without root access, use
pip install --user pandasor create a virtual environment. - Checking environment: Often overlooked, it is vital to ensure you are in the correct virtual environment or using the appropriate Python version for your project setup. Run
which pythonandpip --versionto confirm. - Docker containers: Inside Docker, install pandas in the Dockerfile:
RUN pip install pandas. Do not rely on system-level installations from the host.
Summary
| Symptom | Likely Cause | Fix |
No module named pandas after install | Wrong Python/environment | python3 -m pip install pandas |
| Works in terminal, fails in Jupyter | Different kernel | Install in Jupyter kernel's Python |
| Works in IDE, fails in terminal | Different interpreter | Check which python |
| Fresh machine or container | Not installed | pip install pandas |
| Using conda | Not installed via conda | conda install pandas |
Related reading
- ImportError numpy.core.multiarray failed to import
- Improve subplot size/spacing with many subplots
- Improving k-means clustering
- Improving model training speed in caret R
- ImportError No module named PIL
- ImportError No module named requests
- ImportError No module named requests
- ImportError No module named 'sklearn.lda
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.