ModuleNotFoundError: No Module Named 'Pandas' - Easy Solution in Python

ModuleNotFoundError: No Module Named 'Pandas' - Easy Solution in Python

ModuleNotFoundError - No Module Named ‘Pandas’

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

However, there are more reasons why you might get ModuleNotFoundError: No Module Named ‘Pandas’, and today’s 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, VSCode, Jupyter, or 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.

We already have extensive guides on How to install Pandas and How to install Pandas Specific version, so there’s no need to go into much depth here.

Simply, 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 Panas
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 95% of the time, but if they don’t, continue reading to find the solution.


Other Causes of ModuleNotFoundError - No Module Named ‘Pandas’

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.

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:

Image 1 - Pandas version installed inside a virtual environment (Image by author)

Image 1 - Pandas version installed inside a virtual environment (Image by author)

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:

Image 2 - ModuleNotFoundError - No Module Named ‘Pandas’ error (Image by author)

Image 2 - ModuleNotFoundError - No Module Named ‘Pandas’ error (Image by author)

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 global Python installation doesn’t know of the Pandas dependency.

You’ve Named Your Module ‘pandas.py’

Now, doing this could return all sorts of errors, ModuleNotFoundError being one of them.

Put simply, 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.

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:

Image 3 - Naming a file/module pandas.py (Image by author)

Image 3 - Naming a file/module pandas.py (Image by author)

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.

You’ve 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__)
Image 3 - Declaring a variable named ‘pandas’ (Image by author)

Image 3 - Declaring a variable named ‘pandas’ (Image by author)

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


No Module Named ‘Pandas’ Q&A

We’ll now go over some frequently asked questions about ModuleNotFoundError: No Module Named ‘Pandas’ error and the common solutions for them.

Q: How Do I Fix No Module Named Pandas?

A: 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:

Pip:

pip uninstall pandas
pip install pandas

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.

Q: Why is it Showing No Module Named Pandas?

A: In case you’re using an IDE such as PyCharm, VSCode, 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’ VSCode (Visual Studio Code):

Image 5 - VSCode solution (Image by author)

Image 5 - VSCode solution (Image by author)

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

Image 6 - PyCharm solution (Image by author)

Image 6 - PyCharm solution (Image by author)

Q: Why I Can’t Install Pandas in Python?

A: 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.

Q: How Do I Know if Pandas is Installed?

A: 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:

Image 7 - Pandas installation check (Image by author)

Image 7 - Pandas installation check (Image by author)


Summing up

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.

We hope you’ve managed to install Pandas properly, and can’t wait to see you in the following article.