The Python ModuleNotFoundError: No module named ‘pandas’ in Python error occurs when we didn’t install the library pandas before importing it. To solve this error, run
pip install pandas in your terminal and execute your code again.
Code language: Python (python)
#Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'pandas'
Open your terminal, navigate to your project’s root directory and install the pandas library. The exact instructions you have to execute depend on the OS you are using, though.
Open the terminal application and use pip install pandas or pip3 install pandas (for Python 3). This installation will only work if you have pip.exe installed and pip.exe added to the PATH environment variable. If you don’t, run python -m pip install pandas or python3 -m pip install pandas.
pip is the package installer for Python and comes by default when you install it
- also install the pandas library manually by downloading it from the official website and add the files (unzipped) to
easy-install -U pandasif it is installed on macOS.
- use the native package installer from Linux by running
sudo apt-get python-pandasor
sudo apt-get python3-pandasif you’re using Python 3 on Linux.
Code language: Bash (bash)
# for Python 2 pip install pandas # for python 3 pip3 install pandas # if you don't have pip in your PATH environment variable & use python 2 python -m pip install pandas # if you don't have pip in your PATH environment variable & use python 3 python3 -m pip install pandas # for Linux only & for python 2 sudo apt-get install python-pandas # for Linux only & for python 3 sudo apt-get install python3-pandas
If this process does not solve your issue, explore other explanations for the error ModuleNotFoundError: No module named ‘pandas’:
- Ensure that you have installed the package for the Python version that you are using to run your script.
It’s not rare to have both Python 2 and Python 3 in a single machine; hence, you can easily get confused and install a package for the wrong version. You can run
python3 show pandas or
python3 -m pip show pandas to ensure that the library is indeed installed for the Python version you are using.
If you are working within an IDE such as Visual Studio Code, you can select the Python version you are using by opening the palette –
CTRL + Shift + P or
⌘ + Shift + P
VS Code will then list the available interpreters (including virtual environment) for you to choose from.
Remember that a package available locally won’t be available in a virtual environment. To make sure that the package is available in your virtual environment, you can run
pip3 freeze > requirements.txt and check that pandas is listed in the created file
- If the pandas library is listed but you still get the error
Try to uninstall the pandas library and install it again. Run first
pip uninstall pandas (or
pip3 uninstall pandas) and
pip install pandas (or
pip3 install pandas). This may sound like a strange advice, but sometimes it works ;).
- If you get an AttributeError after solving the ModuleNotFoundError
Ensure that your file is not named
pandas.py or that you didn’t create a variable called
pandas. If you do so, they shadow the module and break your code. For instance, the following code would not run properly:
Code language: Python (python)
#library import import pandas #variable declaration pandas = 1 #try to run a method linked to the library df = pandas.read_csv('file.csv')
- If you get an
ImportErrorinstead of an
These two errors belong to the same error types (based on the error hierarchy defined by Python), but:
- ModuleNotFoundError is more specific and is triggered only if the module is not installed and available for import
- ImportError is more generic and while the package is available, it failed to load.