ModuleNotFoundError: No module named 'xlsxwriter'

Posted in Python by Dirk - last update: Feb 05, 2024

Python raises the ModuleNotFoundError: No module named 'xlsxwriter when it is unable to find the xlsxwriter library. The most likely cause is that you didn’t install xlsxwriter in the environment where you are running your code. Quick fix: install xlsxwriter using: the pip install xlsxwriter command

Possible causes and solutions

XlsxWriter is a Python module for creating and updating Excel XLSX (Excel 2007 and later) files. It is an open-source library that allows Python programs to write data, including charts and images, to Excel files. XlsxWriter is particularly useful for generating reports, working with data that needs to be presented in Excel format, and automating the creation of Excel files.

Sample code:

import xlsxwriter

# Create a new Excel workbook and add a worksheet.
workbook = xlsxwriter.Workbook('example.xlsx')
worksheet = workbook.add_worksheet()

# Write some data to cells.
worksheet.write('A1', 'Hello')
worksheet.write('A2', 'World')

# Add a chart (in this case, a bar chart).
chart = workbook.add_chart({'type': 'bar'})
chart.add_series({'values': '=Sheet1!$A$1:$A$2'})

# Insert the chart into the worksheet.
worksheet.insert_chart('C1', chart)

# Close the workbook to save the changes.
workbook.close()

And when trying to run the code you get the ’no module named error':

Traceback (most recent call last):
  File "myfile.py", line 1, in <module>
ModuleNotFoundError: No module named 'xlsxwriter'

Luckily, it is easy to fix. Below you can find the most common causes and how to fix them:

‘xlsxwriter’ Library is not installed

In your code you are importing the xlsxwriter module

import xlsxwriter

which generates the ModuleNotFoundError: No module named 'xlsxwriter'. The the most common cause for this error: the environment where you are running your code doesn’t have the xlsxwriter library installed.

You can use the following command in the terminal or command prompt to check if the xlsxwriter library is installed:

pip show xlsxwriter

If xlsxwriter is not installed you will get this result:

WARNING: Package(s) not found: xlsxwriter

Solution:

pip install XlsxWriter

‘xlsxwriter’ Module installed in the Wrong Directory

It is possible that you did install the xlsxwriter module, but that the directory where you installed it is not in the Python path.

You can check the Python path in a few different ways:

1. Using sys module in Python:

Open a Python script or the Python interpreter and run the following code:

import sys
print(sys.path)

This will print a list of directories that make up the Python path.

2. Using Command Line

Open a command prompt or terminal and run:

  • Windows
echo %PYTHONPATH%
  • Unix/Linux/macOS
echo $PYTHONPATH

If the PYTHONPATH environment variable is set, it will display the directories in the Python path.

Solution: Move the xlsxwriter module to a directory that is included in the Python path or add the xlsxwriter module’s directory to the sys.path using sys.path.append().

‘xlsxwriter’ not installed in Virtual Environment

Even if you have xlsxwriter installed in on your computer, if you are using a virtual environment you cannot access it from this environment.

If you are using virtual environments, ensure that the virtual environment is activated, and that the xlsxwriter modules is installed within this environment.

Installation is simple:

pip install XlsxWriter

File Naming Conflicts:

It is not really likely - but if you happen to have a xlsxwriter.py file in your environment and you also installed the standard xlsxwriter library it will cause a conflict. Rename your script/module to avoid conflicts with existing module names.

Other articles