How to Fix ModuleNotFoundError: No Module Named ‘Pandas’

ModuleNotFoundError: No module named ‘pandas’ is often thrown when the Python interpreter can’t locate the Pandas library installation. Here’s how to fix it. 

Written by Dario Radečić
Published on Dec. 12, 2024
Developer frustrated with an error in his code
Image: Shutterstock / Built In
Brand Studio Logo

The ModuleNotFoundError: No module named ‘pandas’ error occurs because the Python interpreter can’t locate your installation of the Pandas library. The easiest solution is to make sure Pandas is installed, which you can do with the following shell commands, depending on your Python installation, either pip or Anaconda:

  • Pip: Run pip install pandas
  • Anaconda: Run conda install pandas

How to Fix ModuleNotFoundError: No Module Named ‘Pandas’

The ModuleNotFoundError: no module named ‘Pandas’ most often occurs when the Python interpreter can’t locate the Pandas library installation. The simplest solution is to make sure Pandas is installed with the following command in pip:

# To install the latest stable version of Panas
pip install pandas

# To install a specific version of Pandas, e.g., 1.3.4
pip install pandas==1.3.4

However, there are more reasons why you might get ModuleNotFoundError: No module named ‘pandas’, and this article will go through all of them.

 

How to Install Pandas with Pip and Anaconda

The ModuleNotFoundError: no module named ‘Pandas’ often occurs in PyCharm, VS Code, Jupyter Notebook and any other IDE of your choice. The tool is ultimately irrelevant because it doesn’t cause the error, it only reports the result from the Python interpreter.

If you’re using pip, run one of the following commands to install Pandas:

# To install the latest stable version of Panas
pip install pandas

# To install a specific version of Pandas, e.g., 1.3.4
pip install pandas==1.3.4

Likewise, if you’re using Anaconda, run either of these commands:

# To install the latest stable version of Pandas
conda install pandas

# To install a specific version of Pandas, e.g., 1.3.4
conda install pandas=1.3.4

These commands will work most of the time, but if they don’t, continue reading to find the solution.

More on Software EngineeringError: Cannot Find Module in Node Solved

 

ModuleNotFoundError: No Module Named ‘Pandas’ Causes and Solutions

We’ll now walk you through a series of potential reasons why you’re getting the No Module Named ‘Pandas’ error. Let’s start with the first, most obvious one.

1. Pandas Installed in a Virtual Environment, But You’re Accessing it Globally

We have a virtual environment named pandas-env, which contains the latest development version of Pandas - 2.0.0RC1. This Pandas installation is specific to that environment and isn’t accessible outside it.

Take a look at the following image to verify:

Pandas version installed inside a virtual environment
Pandas version installed inside a virtual environment. | Screenshot: Dario Radečić

If you were to deactivate this environment and import Pandas in a global (system-wide) one, you will get a ModuleNotFoundError: No module named ‘pandas’ error:

ModuleNotFoundError: No module named ‘pandas’ error. | Screenshot: Dario Radečić
ModuleNotFoundError: no module named ‘pandas’ error. | Screenshot: Dario Radečić

Solution

If you install Pandas inside a virtual environment, don’t forget to activate it before working with Pandas. Failing to do so is likely to result in a ModuleNotFoundError, since the global Python installation doesn’t know of the Pandas dependency.

2. The Module Is Named ‘pandas.py’

If you name your Python module pandas or if you create a file called pandas.py, you will shadow the actual library you’re trying to import. Doing this could return all sorts of errors, ModuleNotFoundError being one of them.

To demonstrate, create two files in a folder, main.py and pandas.py. The contents of pandas.py are as follows:

# pandas.py

def sum_nums(a: int, b: int) -> int:
 return a + b

And the contents of main.py are:

# main.py

import pandas as pd

print(pd.__version__)

As you can see, main.py tries to import the Pandas library and print its version, but it imports the pandas.py file since it shadows the actual library. Running main.py would result in the following error:

Naming a file/module pandas.py
Naming a file/module pandas.py. | Screenshot: Dario Radečić

Solution

Make sure your files and folders don’t have the same name as built-in Python libraries, nor the libraries you’ve installed manually, such as Pandas.

A tutorial on how to fix ModuleNotFoundError: No module named ‘pandas’. | Video: Ghost Together

More on Software EngineeringHow to Fix ModuleNotFoundError: No Module Named ‘SKlearn’

3. Declared a Variable ‘pandas’ By Accident

If you import the Pandas library and then somewhere in your script assign a value to a variable named pandas, that variable will shadow the library name.

By doing so, you won’t be able to access all the properties and methods Pandas library has to offer.

Take a look at the following snippet, it imports the Pandas library and then declares a variable pandas and assigns a string to it. You can still print out the contents of the variable, but you can’t access properties and methods from the Pandas library anymore:

# main.py
import pandas

pandas = "Don't do this!"

print(pandas)
print("----------")
print(pandas.__version__)
Declaring a variable named ‘pandas’
Declaring a variable named ‘pandas’. | Screenshot: Dario Radečić

Solution

Be more creative with how you name your variables and make sure the name is different from any module you’ve imported.

And there you have it, a list of many, many ways to install Pandas, and many potential issues and solutions you might encounter. As always, there’s no one-size-fits-all solution, but you’re more than likely to find an answer to ModuleNotFoundError: No module named ‘pandas’ in this article.

Likely, you don’t have the library installed, but if that’s not the case, one of the other explained solutions is almost guaranteed to do the trick.

Frequently Asked Questions

First, make sure that Pandas is installed. Do so by running either pip install pandas or conda install pandas, depending on your environment. If that doesn’t work, try reinstalling Pandas by running the following sets of commands.

For pip:

pip uninstall pandas
pip install pandas

For Anaconda:

conda uninstall pandas
conda install pandas

If neither of those works, make sure you don’t have a file/folder named pandas or pandas.py in your project’s directory, and make sure you haven’t named a variable pandas after importing the library.

In case you’re using an IDE such as PyCharm, VS Code or Jupyter, it’s possible the IDE is not recognizing your Python environment. Make sure the appropriate Python kernel is selected first.

A solution to ModuleNotFoundError: No module named ‘pandas’ VS Code:

VSCode solution
VS Code solution. ' Screenshot: Dario Radečić

A solution to PyCharm ModuleNotFoundError: No Module Named ‘Pandas’:

PyCharm solution
PyCharm solution. ' Screenshot: Dario Radečić

One likely reason you can’t install Pandas in Python is that you don’t have administrative privileges on your system. For example, maybe you’re using a work or college computer and trying to install Pandas there. That system will likely be protected, and it won’t allow you to install anything, Python modules included.

Another potential reason is that you’re maybe behind a corporate firewall. Before installing Pandas, configure the firewall settings manually to allow outgoing connections to the Internet, or talk to a company specialist.

You can verify if Pandas is installed on your system by opening up a Python interpreter and running the following code:

import pandas as pd

pd.__version__

If you don’t see any errors after the library import and if you see the version printed, then you have Pandas installed. Congratulations!

Here’s an example of what you should see:

Pandas installation check
Pandas installation check. ' Screenshot: Dario Radečić
Explore Job Matches.