Installing the lxml
library on Windows can be straightforward if you use the right approach. The lxml
library depends on libxml2
and libxslt
C libraries, but modern installation methods handle these dependencies automatically.
Method 1: Using pip (Recommended)
The simplest and most reliable method is using pip with pre-built wheels:
pip install lxml
If you encounter issues, first update pip to the latest version:
pip install --upgrade pip
pip install lxml
For specific versions:
# Install a specific version
pip install lxml==4.9.3
# Install latest stable version
pip install lxml --upgrade
Method 2: Using conda
If you're using Anaconda or Miniconda, conda often provides more reliable installations:
conda install lxml
Or from the conda-forge channel for the latest version:
conda install -c conda-forge lxml
Method 3: Installing from Wheel Files
If pip fails to find a compatible wheel, manually download one:
- Visit PyPI lxml page
- Download the appropriate
.whl
file for your Python version and architecture - Install the downloaded wheel:
# Example for Python 3.11 on 64-bit Windows
pip install lxml-4.9.3-cp311-cp311-win_amd64.whl
How to identify your system:
# Check Python version and architecture
python -c "import sys; print(f'Python {sys.version_info.major}.{sys.version_info.minor}, {sys.platform}, {sys.maxsize > 2**32 and \"64-bit\" or \"32-bit\"}')"
Method 4: Compiling from Source (Advanced)
Only recommended if you need specific customizations or the latest development version:
Prerequisites:
- Install Microsoft Visual C++ Build Tools:
# Download from: https://visualstudio.microsoft.com/visual-cpp-build-tools/
- Install dependencies:
pip install Cython
- Compile and install:
pip install --no-binary :all: lxml
Troubleshooting Common Issues
Error: "Microsoft Visual C++ 14.0 is required"
# Install Visual C++ Build Tools
# Download from: https://visualstudio.microsoft.com/visual-cpp-build-tools/
Error: "No matching distribution found"
# Update pip and try again
pip install --upgrade pip setuptools wheel
pip install lxml
Error: "Failed building wheel for lxml"
# Try installing from conda instead
conda install lxml
# Or use pre-compiled wheel
pip install --only-binary=lxml lxml
ImportError after installation
# Test the installation
import lxml
print(lxml.__version__)
# Test XML parsing
from lxml import etree
root = etree.Element("root")
print(etree.tostring(root, pretty_print=True).decode())
Virtual Environment Installation
For project isolation, use virtual environments:
# Create virtual environment
python -m venv myproject
myproject\Scripts\activate
# Install lxml in the virtual environment
pip install lxml
Verification
After installation, verify lxml is working correctly:
from lxml import html, etree
# Test HTML parsing
html_content = "<html><body><h1>Test</h1></body></html>"
doc = html.fromstring(html_content)
print(doc.xpath('//h1/text()')[0]) # Output: Test
# Test XML parsing
xml_content = "<root><item>Hello World</item></root>"
root = etree.fromstring(xml_content)
print(root.find('item').text) # Output: Hello World
Performance Note
Pre-built wheels include optimized C extensions and are significantly faster than pure Python alternatives. Always prefer pip or conda installation over compiling from source unless you have specific requirements.