Last modified: Aug 11, 2026

Fix ModuleNotFoundError: No module named openpyxl

Encountering the ModuleNotFoundError: No module named 'openpyxl' error is a common hurdle for Python developers. This error simply means your Python environment cannot locate the openpyxl library. This library is essential for reading and writing Excel files (.xlsx).

Don't worry. This guide provides clear, step-by-step solutions. You will have this fixed in minutes. We will cover the most common causes and the simplest fixes.

What Causes This Error?

The error occurs when the openpyxl package is not installed. It might also happen if you are using the wrong Python interpreter. Sometimes, you might have multiple Python versions on your system. The package could be installed for one version but not the one you are using.

Another common reason is that you are working inside a virtual environment. If you haven't activated the environment, or if the package isn't installed in it, you will see this error. Let's look at the solutions.

Solution 1: Install openpyxl Using pip

The most straightforward fix is to install the package. Open your terminal or command prompt. Then, run the following command:


pip install openpyxl

This command will download and install the latest version of openpyxl. After the installation finishes, try running your script again. The error should be resolved.

If you are using Python 3, you might need to use pip3 instead. This ensures you install the package for the correct Python version. Use this command:


pip3 install openpyxl

For more complex Python setups, you might prefer using the python -m pip command. This is a safer method. It guarantees that pip is used for the same Python interpreter you are running. Here is the command:


python -m pip install openpyxl

Solution 2: Use a Virtual Environment

Virtual environments are best practices for Python projects. They keep dependencies isolated. If you are not using one, start now. First, create a virtual environment in your project directory.


python -m venv venv

Next, activate it. The activation command differs by operating system. On Windows, use:


venv\Scripts\activate

On macOS and Linux, use:


source venv/bin/activate

Now, install openpyxl inside this active environment. Use the pip command from Solution 1. This ensures your project has its own copy of the library. This avoids conflicts with other projects.

Solution 3: Check Your Python Environment

Sometimes, your IDE (like VS Code or PyCharm) uses a different Python interpreter. You must ensure the correct interpreter is selected. In VS Code, press Ctrl+Shift+P (or Cmd+Shift+P on Mac). Then, type "Python: Select Interpreter". Choose the one where you installed openpyxl.

If you are using Jupyter Notebook, the kernel might be different. You can install the package directly from a notebook cell. Use this command:


!pip install openpyxl

The exclamation mark tells the notebook to run it as a shell command. This installs it for the notebook's kernel.

Solution 4: Upgrade pip and openpyxl

An outdated pip might cause installation issues. First, upgrade pip itself. Then, upgrade openpyxl. Run these commands in sequence:


python -m pip install --upgrade pip
python -m pip install --upgrade openpyxl

This ensures you have the latest features and bug fixes. It often solves hidden dependency problems.

Verifying the Installation

After installing, verify it works. Run a quick Python script. Create a new Python file or use the interactive shell. Type the following:


# Import the library to verify installation
import openpyxl

# Create a simple workbook
workbook = openpyxl.Workbook()

# Get the active worksheet
sheet = workbook.active

# Add some data
sheet["A1"] = "Hello"
sheet["B1"] = "World"

# Save the file
workbook.save("example.xlsx")
print("Success! openpyxl is working.")

Run this script. If you see "Success!", the installation is correct. You can now use openpyxl for your Excel tasks. This solves the import error completely.

If you still have issues, check your project's requirements.txt file. Ensure openpyxl is listed there. This helps other developers set up your project easily. You can learn more about managing dependencies in our Python project setup guide.

Common Pitfalls and How to Avoid Them

One common mistake is having multiple Python installations. Use the command where python on Windows or which python on macOS/Linux. This shows which Python you are using. Make sure pip installs to that exact location.

Another pitfall is ignoring the error message. Read the full traceback. It often shows the exact path where Python is looking for modules. This path can tell you which environment is active. If the path is unexpected, you know the interpreter is wrong.

Remember, openpyxl is a third-party library. It is not part of Python's standard library. So, you must install it manually. This is different from built-in modules like os or sys. For more on Python's standard library, see our Python basics article.

Alternative: Use a Requirements File

For larger projects, a requirements file is helpful. Create a file named requirements.txt. Add this line to it:


openpyxl==3.1.2

Then, install all dependencies with one command:


pip install -r requirements.txt

This is a clean and reproducible way to manage packages. It ensures everyone uses the same versions. This prevents future "ModuleNotFoundError" issues.

Conclusion

The ModuleNotFoundError: No module named 'openpyxl' error is easy to fix. Start with the simple pip install command. If that fails, check your virtual environment and interpreter. Always verify your installation with a small test script. By following these steps, you will avoid this error in the future. Now, go ahead and process those Excel files with confidence. For more advanced data handling, you might also want to explore Pandas for data analysis.