How to Fix ModuleNotFoundError: No Module Named Openpyxl in Python

Fariba Laiq Feb 02, 2024
How to Fix ModuleNotFoundError: No Module Named Openpyxl in Python

Every programming language encounters many errors. Some occur at the compile time, some at run time.

This article will discuss Python’s No module named 'openpyxl' error. A ModuleNotFoundError arises when the module we are importing is not installed or is located in another directory.

Openpyxl is a library in Python that reads and writes data from an Excel file.

Causes of the No module named 'openpyxl' Error in Python

Module Not Installed

The most common cause of this error is that the module openpyxl is not installed, and we are trying to import it into our program.

To fix this error, we need to install the module correctly. If we use Anaconda, we will use the following command to install the openpyxl module.

#Python 3.x
conda install -c anaconda openpyxl

If we are not using Anaconda, we can use the pip command to install the openpyxl module.

If we are using Python 2, use the following command.

#Python 2.x (Windows)
pip install openpyxl

If we are using Python 3, use the following command.

#Python 3.x (Windows)
pip3 install openpyxl

If pip is not set in your PATH environment variable:

python -m pip install openpyxl

On Centos:

yum install openpyxl

On Ubuntu:

sudo apt-get install openpyxl

The error can also arise if we install the openpyxl with pip if you are using Python 3 and vice versa. We should install the openpyxl using the correct pip version.

We will use the following command to check whether the openpyxl module is installed successfully.

#Python 3.x
pip list

It will show us the list of installed modules. If we find the openpyxl module in the list, it is installed successfully.

Incorrect Module Path

If the module is installed correctly, but we still face the error, the module and our Python code are located in different directories.

For example, the directory structure looks like the following.

code.py
my_folder
---module.py

In this case, we can solve the error by correctly importing the module from the other directory using the following syntax.

# Python 3.x
import my_folder.module.py
Author: Fariba Laiq
Fariba Laiq avatar Fariba Laiq avatar

I am Fariba Laiq from Pakistan. An android app developer, technical content writer, and coding instructor. Writing has always been one of my passions. I love to learn, implement and convey my knowledge to others.

LinkedIn

Related Article - Python Error