How to Solve "ModuleNotFoundError: No module named 'lxml'" in Python
The error ModuleNotFoundError: No module named 'lxml'
means that Python can not find the lxml
library. lxml
is a powerful and feature-rich library for processing XML and HTML in Python. This error almost always means you haven't installed it, or you've installed it in the wrong Python environment.
This guide provides a step-by-step guide to installing lxml
and resolving this common error.
Understanding the Error
The ModuleNotFoundError
is a very common Python error. It means your Python code is trying to import lxml
, but the Python interpreter can't find a package named lxml
in any of the locations it searches for installed packages.
Solution 1: Install lxml
with pip
(in a Virtual Environment!)
The best practice and strongly recommended approach is to use a virtual environment. This keeps your project's dependencies isolated.
-
Step 1: Create a Virtual Environment (if you don't have one):
python3 -m venv venv # Or: python -m venv venv , or: py -m venv venv
This creates a directory named
venv
(you can use any name) that contains a self-contained Python environment. -
Step 2: Activate the Virtual Environment:
-
macOS / Linux:
source venv/bin/activate
-
Windows (Command Prompt):
venv\Scripts\activate.bat
-
Windows (PowerShell):
venv\Scripts\Activate.ps1
- If you get a "running scripts is disabled" error in PowerShell, you may need to temporarily change the execution policy:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
- Then run
venv\Scripts\Activate.ps1
again.
- If you get a "running scripts is disabled" error in PowerShell, you may need to temporarily change the execution policy:
After activation, your command prompt should show
(venv)
(or whatever you named your environment) at the beginning of the line. -
-
Step 3: Install
lxml
:pip install lxml
noteIf
pip
doesn't work, try these:pip3 install lxml # If pip is mapped to Python2
python -m pip install lxml # Use the python executable
python3 -m pip install lxml
py -m pip install lxml # Windows Python launcher- If you encounter a permissions error, try
pip install lxml --user
(though this is less desirable than using a virtual environment).
- If you encounter a permissions error, try
Troubleshooting
If you're still getting the error after installing lxml
, consider these possibilities:
Virtual Environment Activation
Make absolutely sure your virtual environment is activated. The command prompt should show the environment name (e.g., (venv)
). If it doesn't, you're installing into the global environment, which is what you're trying to avoid.
IDE Configuration (VS Code, PyCharm)
Your IDE (like VS Code or PyCharm) might be using a different Python interpreter than your terminal.
- VS Code: Press
Ctrl+Shift+P
(orCmd+Shift+P
on macOS), type "Python: Select Interpreter", and choose the interpreter within your virtual environment (it will usually have the environment name in the path). - PyCharm: Go to
File > Settings > Project > Python Interpreter
(orPyCharm > Preferences > Project > Python Interpreter
on macOS) and select the correct interpreter (your virtual environment).
Conflicting Installations / Multiple Python Versions
If you have multiple Python versions installed without using virtual environments, you could have installed lxml
for the wrong one. Always use virtual environments to avoid this.
Incorrect Filenames/Variable Names
Never name your Python files lxml.py
. This will conflict with the installed library. Also, avoid creating variables named lxml
before you import the module.
Restarting Your IDE/Kernel
Sometimes, especially in interactive environments like Jupyter notebooks, or after installing a new package, you need to restart the Python kernel or the entire IDE for the changes to take effect.
Reinstalling
If none of the above steps help, try uninstalling and reinstalling the package, to make sure there are no corrupted files.
pip uninstall lxml
pip install lxml
Using lxml
(Example)
Once lxml
is installed correctly, you can import and use it:
from lxml import etree
html_string = """
<div>
<ul>
<li>tutorial</li>
<li>reference</li>
<li>com</li>
</ul>
</div>
"""
result = etree.fromstring(html_string).text_content()
print(result)
Output:
tutorial
reference
com
Installing lxml
in Specific Environments
Anaconda
conda install -c anaconda lxml
Jupyter Notebook
Within a notebook cell:
!pip install lxml
Conclusion
The ModuleNotFoundError: No module named 'lxml'
error is almost always resolved by installing the lxml
package using pip
, preferably within a virtual environment. If problems persist, check your IDE's Python interpreter, ensure you're using the correct virtual environment, and avoid filename conflicts.