Last modified: Oct 06, 2026
Install PyQuery in Python: A Quick Guide
PyQuery is a Python library that lets you parse HTML and XML using jQuery-like syntax. It is fast, lightweight, and beginner-friendly. Installation is usually simple, but some users hit dependency errors. This guide walks you through every step.
What Is PyQuery?
PyQuery brings the power of jQuery selectors to Python. You can load HTML from a string, a file, or a URL. Then you can select elements with CSS selectors and extract data easily. It uses the lxml library under the hood for speed[reference:0].
PyQuery is ideal for web scraping and data extraction. It feels natural if you already know jQuery. If you are new to it, the syntax is still easy to learn.
Prerequisites
Before installing PyQuery, make sure you have Python 3.6 or newer. You can check your version with this command.
python --version You also need pip, Python's package installer. Most Python installations include pip automatically. If you do not have pip, install it from the official Python website[reference:1].
Install PyQuery Using pip
The standard way to install PyQuery is through pip. Open your terminal or command prompt and run one of the following commands.
pip install pyquery If you have multiple Python versions, use the module form to avoid confusion.
python -m pip install pyquery This command installs PyQuery along with its dependencies, lxml and cssselect[reference:2]. The process usually takes less than a minute.
Install a Specific Version
Sometimes the latest version may have compatibility issues. You can pin a specific version instead. For example, to install PyQuery 2.0.0, run this.
python -m pip install pyquery==2.0.0 Check the PyPI page for available versions. Using a pinned version helps keep your project stable[reference:3].
Install PyQuery in a Virtual Environment
It is a best practice to use a virtual environment. This keeps your project dependencies isolated from your system Python. First, create the environment.
python -m venv myenv Then activate it. On Windows, run the activate script.
myenv\Scripts\activate On macOS and Linux, use the source command.
source myenv/bin/activate After activation, install PyQuery using pip. The library will be installed only inside the virtual environment[reference:4].
pip install pyquery Verify the Installation
After installation, confirm that PyQuery works. Open a Python shell and run this import statement.
from pyquery import PyQuery as pq print("PyQuery installed successfully!") If you see the success message, you are ready to go. If not, check the troubleshooting section below.
Common Installation Errors and Fixes
Most PyQuery installation problems come from missing system dependencies. The lxml library needs development headers for XML and XSLT. Without them, pip cannot build the package.
Error: ImportError - cannot import name 'etree' from 'lxml'
This error appears on Python 3.12 and some lxml versions. It is a compatibility issue between PyQuery and lxml[reference:5]. The fix is to install a compatible lxml version.
pip install lxml==4.9.4 pip install pyquery If that does not work, try upgrading lxml to the latest release.
pip install --upgrade lxml Error: Missing libxml2 and libxslt
On Linux, you may see build errors about missing headers. Install the development packages first, then retry pip[reference:6].
sudo apt-get install libxml2-dev libxslt1-dev python-dev On CentOS or Fedora, use yum instead.
sudo yum install libxslt-devel libxml2-devel libxml2 libxslt After installing the system dependencies, run pip install again. The error should disappear[reference:7].
Error: ModuleNotFoundError - No module named 'pyquery'
This usually means PyQuery is installed for a different Python version. Check which Python pip is using.
pip --version python --version If they do not match, use python -m pip install pyquery to target the correct interpreter. This ensures the package goes to the same Python you run[reference:8].
Error: Microsoft Visual C++ 14.0 is required (Windows)
On Windows, lxml may need the Microsoft C++ Build Tools. Download and install "Build Tools for Visual Studio" from the Microsoft website. During installation, select the "Desktop development with C++" workload.
Alternatively, use a pre-built wheel. Recent lxml versions provide wheels for Windows, so upgrading pip may solve the issue.
python -m pip install --upgrade pip pip install pyquery Quick Usage Example
Once installed, PyQuery is easy to use. This example fetches a webpage and extracts the title.
import requests from pyquery import PyQuery as pq # Fetch the HTML content r = requests.get("https://example.com") # Load it into PyQuery doc = pq(r.content) # Extract the title text title = doc("title").text() print(title) The output shows the page title.
Example Domain You can also load HTML directly from a string or a file. The PyQuery() constructor accepts all three sources[reference:9].
# From a string doc = pq("Hello
") # From a file doc = pq(filename="example.html") # From a URL (no requests needed) doc = pq(url="https://example.com") Use the .text() method to get the text content of selected elements. Use the .attr() method to read an attribute value.
# Get text from the first h1 tag heading = doc("h1").text() # Get the href attribute of a link link = doc("a").attr("href") These methods make data extraction fast and readable[reference:10].
Uninstalling PyQuery
If you need to remove PyQuery, use pip uninstall. This is useful when you want to reinstall a clean version.
pip uninstall pyquery Confirm the uninstallation when prompted. You can also remove the virtual environment folder if you no longer need it.
Conclusion
Installing PyQuery in Python is a quick process. Use pip install pyquery for most cases. If you hit errors, the problem is usually missing lxml dependencies or a Python version mismatch. Install the system development packages, pin a compatible lxml version, or use a virtual environment to avoid conflicts. Once installed, PyQuery gives you a clean, jQuery-like way to parse HTML and extract data. It is a valuable tool for any web scraping or data processing project.