. Advertisement .
..3..
. Advertisement .
..4..
The problem “ModuleNotFoundError: No module named ‘xlsxwriter'” in Python often occurs when we forget to install the ‘xlsxwriter’ module before using/importing it. Check out our solutions below to see which one works best for you.
What causes the error “ModuleNotFoundError: No module named ‘xlsxwriter'” in Python ?
Reproduce the “ModuleNotFoundError: No module named ‘xlsxwriter'” in Python
This is a common error that Python developers usually make when they try to import a module that is not a built-in module.
Output example:
Traceback (most recent call last):
File "F:\tutopal\test.py", line 1, in <module>
import xlsxwriter
ModuleNotFoundError: No module named 'xlsxwriter'
What is the ‘xlsxwriter’ module?
XlsxWriter is a Python module that allows you to create files in the Excel 2007+ XLSX file format.
XlsxWriter can be used to add text, numbers, formulas, and hyperlinks to multiple worksheets, and it includes formatting and other features.
See the full official documentation here.
What are the reasons?
The error “ModuleNotFoundError: No module named ‘xlsxwriter'” in Python can occur for various reasons, but the most common is that we attempt to import the module without the ‘xlsxwriter’ package installed.
How to fix this error?
Solution 1: Install the ‘xlsxwriter’ module
Since xlsxwriter is not a built-in module in Python, you need to install it first using the pip installer and then use it.
To install, open the terminal in your project’s root directory and type:
pip install xlsxwriter
If you are using python 3, type in the terminal:
pip3 install xlsxwriter
If you get permissions error, type in the terminal:
sudo pip3 install xlsxwriter
If the pip is not set as environment variable PATH, type in the terminal:
python -m pip install xlsxwriter
If you are using Python 2 on Linux, type in the terminal:
sudo pip install xlsxwriter
If you are using Python 3 on Linux, type in the terminal:
sudo pip3 install xlsxwriter
On Ubuntu, type in the terminal:
sudo apt-get install xlsxwriter
In Anaconda, type in the terminal:
conda install -c conda-forge xlsxwriter
You can also clone ‘xlsxwriter’ from Github:
The XlsxWriter source code and bug tracker are available on GitHub in the XlsxWriter repository. You can clone the repository and install from it by typing respectively these commands:
$ git clone https://github.com/jmcnamara/XlsxWriter.git
$ cd XlsxWriter
$ python setup.py install
Solution 2: Verify the correct version of Python in the IDE
If you are still getting the error after installing the module package, try to verify the Python version that the IDE is using.
Example:
- In VSCode, press CTRL + Shift + P on Windows or ⌘ + Shift + P on Mac to open the command palette.
- Then select the Python interpreter and select the right version of Python.
Solution 3: If using a virtual environment, make sure to install ‘xlsxwriter’ in your virtual environment
Some IDEs use the global Python, but somes install their own virtual environment of Python instead. In that case, ensure that ‘xlsxwriter’ is installed in your virtual environment.
- Step 1: Activate the virtual environment
Windows command: venv\Scripts\activate.bat
Windows powershell: venv\Scripts\Activate.ps1
Linux: source venv/bin/activate
- Step 2: Install ‘xlsxwriter’ in your virtual environment
pip install xlsxwriter
Solution 4: Make sure you do not declared any variable named ‘xlsxwriter’
Testing
When everything has been installed and set up successfully, you can create a program like this to confirm that the module works correctly:
Example:
import xlsxwriter
workbook = xlsxwriter.Workbook('hello.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Welcome to Tutopal!')
workbook.close()
This program will create an output file called hello.xlsx that looks like this:
......... ADVERTISEMENT .........
..8..
Conclusion
The “ModuleNotFoundError: No module named ‘xlsxwriter'” in Python is a common problem but not hard to fix. In most cases, we usually forget to install the module before importing it, which can be solved easily with Solution 1 above. If the error is still not resolved, try to uninstall and reinstall the package.
Read more
Leave a comment