ModuleNotFoundError: No module named 'pandas' means the Python running your script cannot see pandas. Usually it is installed — just into a different Python than the one executing the code. Two commands tell you which situation you are in, and the fix follows from that.
The fix that works most of the time#
python -m pip install pandas
The python -m prefix is the whole point. Plain pip is a separate program that might belong to a different Python installation; python -m pip is guaranteed to install into the interpreter you just ran.
The two-command diagnosis#
python -c "import sys; print(sys.executable)"
python -m pip show pandas
The first prints the exact interpreter that runs your code. The second says whether pandas is installed for that interpreter, and where.
- Both agree and pandas is listed — the problem is somewhere else, see the naming section below.
- Nothing found — install with
python -m pip install pandas. - Installed but the location does not match the interpreter path — you have two Pythons.
To see how many Pythons you have:
where python # Windows
which -a python3 # macOS and Linux
Virtual environments#
A virtual environment is deliberately isolated: globally installed packages are invisible inside it.
python -m venv .venv
source .venv/bin/activate # macOS and Linux
.venv\Scripts\activate # Windows PowerShell
python -m pip install pandas
python -c "import pandas; print(pandas.__version__)"
Your prompt should now start with (.venv). If it does not, activation did not work, and everything you install is going somewhere else.
Record what you installed so the environment can be rebuilt:
python -m pip freeze > requirements.txt
python -m pip install -r requirements.txt
Conda#
Inside a conda environment, install with conda where you can:
conda activate myenv
conda install pandas
If conda activate itself fails, run conda init and open a new terminal — the shell function it installs is what makes activation possible.
Mixing conda and pip in one environment works but should be done in order: conda packages first, pip only for what conda does not have.
VS Code#
VS Code chooses its own interpreter, independent of your terminal. That is the single most common reason a script runs in the terminal and fails in the editor.
- Ctrl+Shift+P, then Python: Select Interpreter
- Choose the one inside your project’s
.venv - Check with a cell or a script:
import sys; print(sys.executable)
The interpreter is also shown in the status bar at the bottom, which is worth glancing at whenever an import surprises you.
Jupyter#
A notebook runs a kernel, which is its own environment. Install from inside the notebook so there is no ambiguity:
import sys
!{sys.executable} -m pip install pandas
Then restart the kernel — Python caches modules at import time, so a freshly installed package is not visible until it restarts.
To make an environment available as a kernel properly:
python -m pip install ipykernel
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
Docker#
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "main.py"]
Installing pandas on your machine does nothing for the container. If you added it to requirements after building, rebuild the image — Docker caches that layer and will not notice the change otherwise.
The naming mistake#
If pandas is definitely installed and the import still fails, check for a file of your own shadowing it:
ls pandas.py
A file named pandas.py in your working directory is found before the real library, because the current directory comes first on the import path. The symptom is often stranger than a plain import error — partial imports, missing attributes, odd tracebacks.
The same applies to a pandas/ folder, and to a stale __pycache__ left behind after you rename the file. Delete both.
Permission errors#
ERROR: Could not install packages due to an OSError: [Errno 13] Permission denied
Do not reach for sudo pip install — it writes into the system Python that your operating system depends on. Use a virtual environment, or install for your user only:
python -m pip install --user pandas
Questions people ask#
Why does pip say “already satisfied” but the import still fails?
Pip is installing for a different interpreter than the one running your script. Compare pip show pandas against python -c "import sys; print(sys.executable)" — the paths will not match.
Should I use pip or conda?
Whichever created the environment. In a conda environment prefer conda install; in a venv use pip. Alternating between them for the same package is what breaks environments.
Do I need a virtual environment for a small script?
Not strictly, but it takes ten seconds and prevents every version conflict you would otherwise meet later. The habit costs almost nothing.
How do I install a specific version?
python -m pip install "pandas==2.2.0" for an exact version, or "pandas>=2.0,<3.0" for a range. Both work in a requirements file too.
Where to go next#
- Python virtual environments — the permanent fix for this class of error.
- ModuleNotFoundError for mysql — the same diagnosis, different package.
- Why is my Python code not working? — reading errors methodically.