How to Resolve "ModuleNotFoundError: No Module Named 'httpx'" in Python
The ModuleNotFoundError: No module named 'httpx' error occurs when Python cannot find the httpx library in your current environment. httpx is a modern, fully-featured HTTP client for Python that supports both synchronous and asynchronous requests, HTTP/2, and provides a clean API similar to the popular requests library.
In this guide, you will learn what causes this error and how to resolve it quickly across different environments and setups.
What Causes This Error?ā
The error is triggered when you try to import httpx but Python cannot locate it:
import httpx
Output:
Traceback (most recent call last):
File "main.py", line 1, in <module>
import httpx
ModuleNotFoundError: No module named 'httpx'
The most common reasons are:
- httpx is not installed in the current Python environment.
- Incorrect module name: a typo or wrong casing in the import statement.
- Wrong Python environment: httpx is installed in a different interpreter or virtual environment than the one running your script.
Solution 1: Install httpxā
The primary fix is installing the package with pip:
pip install httpx
To ensure you install it for the correct Python interpreter:
# ā
Recommended: guarantees the right Python
python -m pip install httpx
# For Python 3 explicitly
python3 -m pip install httpx
Installing With Optional Dependenciesā
httpx supports optional features that require extra packages:
# HTTP/2 support
pip install "httpx[http2]"
# SOCKS proxy support
pip install "httpx[socks]"
# All optional dependencies
pip install "httpx[http2,socks]"
Verify the Installationā
python -m pip show httpx
Expected output:
Name: httpx
Version: 0.27.0
Summary: The next generation HTTP client.
Location: /home/user/venv/lib/python3.11/site-packages
Requires: anyio, certifi, httpcore, idna, sniffio
Or verify directly in Python:
import httpx
print(httpx.__version__)
Output:
0.27.0
Solution 2: Fix the Module Nameā
Python is case-sensitive. The correct import uses all lowercase:
# ā Incorrect: wrong casing
import Httpx
import HTTPX
import HttpX
# ā Incorrect: typo
import htpx
import httx
# ā
Correct
import httpx
Common httpx import patterns:
# Import the module
import httpx
# Import the async client
from httpx import AsyncClient
# Import both sync and async clients
from httpx import Client, AsyncClient
Solution 3: Activate Your Virtual Environmentā
If httpx is installed inside a virtual environment, that environment must be active when you run your script:
# Linux / macOS
source venv/bin/activate
# Windows
venv\Scripts\activate
# Then verify httpx is available
python -m pip show httpx
If httpx is not installed in the virtual environment:
# Install within the activated environment
pip install httpx
Installing httpx globally does not make it available inside a virtual environment, and vice versa:
# ā Installed globally, but venv is active
pip install httpx # Goes to global site-packages
source venv/bin/activate
python -c "import httpx" # ModuleNotFoundError!
# ā
Install after activating the environment
source venv/bin/activate
pip install httpx # Goes to venv site-packages
python -c "import httpx" # Works!
Solution 4: Check Your IDE's Python Interpreterā
If the error appears in your IDE but not in the terminal (or vice versa), the IDE may be using a different Python interpreter.
VS Codeā
- Press
Ctrl+Shift+P(orCmd+Shift+Pon macOS). - Type "Python: Select Interpreter".
- Choose the interpreter or virtual environment where httpx is installed.
PyCharmā
- Go to File ā Settings ā Project ā Python Interpreter.
- Verify the selected interpreter has httpx listed.
- If not, click the + button to install it directly from PyCharm.
Quick Diagnostic Scriptā
Run this to immediately identify the problem:
import sys
print(f"Python: {sys.executable}")
print(f"Version: {sys.version}")
try:
import httpx
print(f"\nā
httpx {httpx.__version__} is installed and importable.")
# Quick functionality test
response = httpx.get("https://httpbin.org/get")
print(f" Test request status: {response.status_code}")
except ModuleNotFoundError:
print(f"\nā httpx is NOT installed.")
print(f" Fix: {sys.executable} -m pip install httpx")
except Exception as e:
print(f"\nā ļø httpx is installed but encountered an error: {e}")
Output (when httpx is installed):
Python: /home/user/project/venv/bin/python
Version: 3.11.5
ā
httpx 0.27.0 is installed and importable.
Test request status: 200
Output (when httpx is NOT installed):
Python: /home/user/project/venv/bin/python
Version: 3.11.5
ā httpx is NOT installed.
Fix: /home/user/project/venv/bin/python -m pip install httpx
Common Scenarios and Fixesā
| Scenario | Diagnostic | Fix |
|---|---|---|
| httpx not installed | pip show httpx shows nothing | python -m pip install httpx |
| Typo in import | Import says Httpx or htpx | Change to import httpx |
| Wrong virtual environment | which python points to wrong path | Activate the correct venv |
| IDE using wrong interpreter | Check IDE interpreter settings | Select the correct interpreter in IDE settings |
| Multiple Python versions | python --version vs python3 --version | Use python3 -m pip install httpx |
| Docker / CI environment | Package not in requirements.txt | Add httpx to requirements.txt |
Ensuring httpx Is in Your Project Dependenciesā
For reproducible environments, always include httpx in your dependency files:
requirements.txt:
httpx>=0.27.0
pyproject.toml (with Poetry):
[tool.poetry.dependencies]
httpx = "^0.27.0"
Pipfile (with Pipenv):
[packages]
httpx = ">=0.27.0"
Then install from the dependency file:
pip install -r requirements.txt
Conclusionā
The ModuleNotFoundError: No module named 'httpx' is resolved by ensuring httpx is installed in the correct Python environment.
Run python -m pip install httpx to install it, verify with python -m pip show httpx, and confirm your IDE and terminal are using the same Python interpreter.
Always use lowercase httpx in your import statements, and include the package in your project's dependency file for consistent, reproducible environments across development and production.