How to Resolve Python "ModuleNotFoundError: No module named 'xlsxwriter'"
The ModuleNotFoundError: No module named 'xlsxwriter'
is a common error encountered when trying to use the XlsxWriter
library in Python, which is used for creating Excel .xlsx
files. This error simply means that the Python interpreter cannot find the xlsxwriter
package when executing your import xlsxwriter
statement. This typically happens because the package isn't installed or isn't accessible in the current Python environment.
This guide provides a clear walkthrough of the causes and step-by-step solutions to install XlsxWriter
correctly.
Understanding the Error: Python's Import System
When Python sees import xlsxwriter
, it searches through configured directories (sys.path
) to find the package. These include the script's directory, standard library paths, and site-packages
(where third-party libraries like XlsxWriter
are installed). If the xlsxwriter
package isn't located in any directory accessible to the active Python interpreter, the ModuleNotFoundError
occurs.
Common Causes of the Error
- Package Not Installed: You haven't run
pip install XlsxWriter
. - Incorrect Python Environment:
XlsxWriter
was installed in a different Python version or virtual environment than the one running your script. - Virtual Environment Not Activated: You installed the package globally but are running the script from an unactivated virtual environment (or vice-versa).
- IDE Misconfiguration: Your IDE (VS Code, PyCharm) is using a Python interpreter that doesn't have
XlsxWriter
installed. - Filename Shadowing: You named your script
xlsxwriter.py
. - Variable Shadowing: You used
xlsxwriter
as a variable name before the import.
Solution 1: Install the XlsxWriter
Package (pip/conda)
The primary fix is to install the package into your project's active Python environment. Open your terminal/command prompt, activate your virtual environment if you use one, and run the appropriate command:
-
Using
pip
(Standard):pip install XlsxWriter # Note the capitalization 'XlsxWriter'
# Or use pip3 if needed
pip3 install XlsxWriter
# Or use python -m pip if pip is not directly in PATH
python -m pip install XlsxWriter
python3 -m pip install XlsxWriter
py -m pip install XlsxWriter # Windows 'py' launcher
# If permission errors occur:
pip install XlsxWriter --user
# Or (use with caution):
sudo pip3 install XlsxWriter # Linux/macOS system-widenotePay attention to the capitalization:
pip install XlsxWriter
-
Using
conda
(for Anaconda/Miniconda environments):# Activate your conda environment first: conda activate your_env_name
conda install -c anaconda xlsxwriter
# Or from conda-forge:
conda install -c conda-forge xlsxwriter
After successful installation, try running your script again.
Solution 2: Verify the Python Environment
Ensure the installation environment matches the execution environment.
Checking Python Version
Verify your python
and pip
versions.
python --version
pip --version
# Or:
python3 --version / pip3 --version
Use the corresponding pip
if you have multiple Python versions installed (e.g., pip3.10 install XlsxWriter
).
Using Virtual Environments (Highly Recommended)
Virtual environments (venv
, conda env
) are essential for managing project dependencies reliably.
- Create (if needed):
python3 -m venv venv
- Activate: (Use OS-specific command:
source venv/bin/activate
,venv\Scripts\activate.bat
, etc.) - Install:
pip install XlsxWriter
(while activated) - Run Script:
python your_script.py
(while activated)
Checking IDE Interpreter Settings (VS Code, PyCharm)
Configure your IDE to use the Python interpreter located within your activated virtual environment.
- VS Code:
Ctrl+Shift+P
(orCmd+Shift+P
) -> "Python: Select Interpreter" -> Choose the Python from yourvenv
folder. - PyCharm:
File
->Settings
/Preferences
->Project
->Python Interpreter
. Select or add the interpreter from your virtual environment.
Solution 3: Check for Filename/Variable Shadowing
- Filename: Ensure you haven't named your script
xlsxwriter.py
. Rename it if necessary. - Variable Name: Avoid using
xlsxwriter
as a variable name before the import statement.
Debugging Steps
If the error persists after installation:
Check if the Package is Installed (pip show
)
Run this in your activated environment's terminal:
pip show XlsxWriter # Check with correct capitalization
# Or: python -m pip show XlsxWriter
Verify it's found and check the Location:
matches your environment's site-packages
.
Restart IDE / Kernel / Script
Close and reopen tools; restart Jupyter kernels.
Reinstall / Upgrade the Package
pip uninstall XlsxWriter
pip install XlsxWriter
# Or upgrade:
pip install --upgrade XlsxWriter
Basic XlsxWriter
Usage Example
After installing XlsxWriter
correctly, you can import and use it:
import xlsxwriter
import os # For cleanup
filename = 'demo_workbook.xlsx'
try:
# ✅ Import works after installation
print("Import successful. Creating workbook...")
# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook(filename)
worksheet = workbook.add_worksheet()
# Write some data.
worksheet.write('A1', 'Hello')
worksheet.write('B1', 'World')
worksheet.write(1, 0, 123) # Row 2, Col 1 (zero-based)
worksheet.write(1, 1, 45.67) # Row 2, Col 2
# Close the workbook (saves the file).
workbook.close()
print(f"Workbook '{filename}' created successfully.")
except ModuleNotFoundError:
print(f"ERROR: Still cannot find xlsxwriter.") # Should not happen now
except Exception as e:
print(f"An error occurred during workbook creation: {e}")
finally:
# Clean up the created file
if os.path.exists(filename):
# You might want to comment this out to inspect the file
os.remove(filename)
print(f"Cleaned up {filename}")
Platform/Tool Specific Installation Summary
The core command is typically pip install XlsxWriter
or conda install ... xlsxwriter
.
- Execute this command within the specific terminal or environment tool you are using (standard Terminal/CMD, VS Code integrated terminal, PyCharm terminal, Anaconda Prompt, Jupyter terminal or notebook cell with
!
). - Ensure any required virtual environment is activated before running the install command.
Conclusion
The ModuleNotFoundError: No module named 'xlsxwriter'
indicates that the XlsxWriter
library is not installed or accessible in your current Python environment.
The primary solution is to:
- Activate your project's virtual environment.
- Install the package using
pip install XlsxWriter
(note the capitalization) or the appropriateconda
command. - Verify that your IDE is using the interpreter from that same virtual environment.
Following these steps carefully will resolve the import error and allow you to create Excel files using XlsxWriter
.