添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
想出家的羽毛球  ·  python如何给Excel排序 | ...·  21 小时前    · 
腹黑的弓箭  ·  通过openpyxl的delete_rows ...·  2 周前    · 
坏坏的猴子  ·  Delete ...·  2 周前    · 
干练的充电器  ·  observablecollection ...·  1 月前    · 
精明的大海  ·  毒枭 1-3季 [2015][9.3分] ...·  2 月前    · 
彷徨的枇杷  ·  Toshiba TLP-X3000 ...·  2 月前    · 
苦恼的冲锋衣  ·  FM2023 Licensing and ...·  4 月前    · 
Jay Shaw /
modulenotfounderror_title.png

Are you stuck at the “ModuleNotFoundError: no module named openpyxl” error? People beginning with Python often get stuck when they start to work with the openpyxl library . This is an industry-wide problem that pops up on screens of many coders starting out with Python.

This often happens due to version errors between Python and Openpyxl or due to incorrect installation. Unable to find the right solution can be a head-scratching problem, that’s why in this article we will be going through the most common errors coders encounter with openpyxl and derive solutions to each of those errors.

What is openpyxl?

The absence of a Python library to read Open XML formats created the need for openpyxl which is the current industry standard Python library to read and write excel format files.

The formats supported are: xlsx/xlsm/xltx/xltm.

Most of the time, errors encountered with openpyxl are caused due to incorrect installation. And these errors can get resolved with a simple reinstallation. Let’s look at the correct way to install openpyxl.

Installing openpyxl through package managers

By using pip

The most simple way to install any Python library is by using pip . Pip is a system software written in Python that installs and manages your Python libraries through an open-source library. Installing through pip requires knowledge of your Python version.

If you are using Python2 , the syntax to install any library is:

pip install 'package_name'

Using just ‘pip’ will install any Python package for Python2 versions.

To install it for Python3, write:

pip3 install 'x'

Note: In newer versions of pip, mainly versions north of 3.0, pip installs packages for Python3 by default when just ‘pip’ is typed. This anomaly is found in the most newer versions of Python, and in case you are encountering it, it is advisable to degrade your Python version.

In case you have more than one Python version in your system and you are not sure if vanilla pip3 will fetch the correct version, you can use this syntax instead:

python3 -m pip install --user xlsxwriter

By using Conda

Many coders prefer using Conda over pip , for its virtual environment features. If you want to install openpyxl using Conda, type the following syntax:

conda install -c anaconda openpyxl
conda install openpyxl

The second syntax is mainly for the latest Conda versions (Conda 4.7.6 or higher)

Installing as per your OS

Let’s look at the different ways to install based on which operating system you use!

1. Windows

If you are using windows then the following packages are required to read and write excel files. To install them, type the following syntax:

pip install openpyxl
pip install --user xlsxwriter
pip install xlrd==1.2.0

2. Ubuntu

Linux distributions are better-suited to Python, but you may still run into some errors due to system bugs or bad installation. The safe and correct way to install openpyxl in Ubuntu is to do the following.

Type the syntax below in your terminal:

For Python2:

sudo apt-get install python-openpyxl

For Python3:

sudo apt-get install python3-openpyxl

The above case is similar to the example at the top. A lot of times, incorrect installation is caused due to the installation of openpyxl for Python2 into the systems with Python3.

Knowing your Python versions can solve most of the problem hands-on, and if the problem still persists then degrading your python version and reinstalling packages is another option.

Script path error

Many times, even after a correct installation, openpyxl may throw ‘modulenotfounderror’. It does not matter what installation manager you used, as the package gets installed correctly but the package manager installs it in some other directory. This mainly happens for a few reasons:

  • Your Python scripts are kept in a different directory and your package manager installs them in a different one.
  • A recent update might have changed the name or path of the directory
  • A Manual installation might have created more than one script folder
  • Your system cannot identify the correct script directory.

To resolve this issue, use the following syntax in the terminal:

import sys
sys.append(full path to the site-package directory)

Note: The above code redirects Python to search for import packages from the given path directory. It’s not a permanent solution but rather a turnaround method.

Completely eradicating this issue is a lengthy process as it requires identifying all the script directories and adding them to ‘path’ (The path of script directory where packages are installed). To avoid errors like this, it is important that we identify and remember where our scripts are getting installed. Knowing where the displacement has happened can solve half the problems, and the other half can get solved by installing packages that are compatible with your python version.

Conclusion

In this article, we have learned about the errors people face with openpyxl and the different ways through which it can get solved. These methods do not just work for openpyxl, but many python library packages that show ‘ filenotfounderror ‘. Most of the time, errors occur due to incompatible versions, bad installations, or installation in the wrong directory.