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 httpxOutput:
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 httpxTo ensure you install it for the correct Python interpreter:
python -m pip install httpx
python3 -m pip install httpxInstalling With Optional Dependencies
httpx supports optional features that require extra packages:
pip install "httpx[http2]"
pip install "httpx[socks]"
pip install "httpx[http2,socks]"Verify the Installation
python -m pip show httpxExpected 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, sniffioOr verify directly in Python:
import httpx
print(httpx.__version__)Output:
0.27.0Solution 2: Fix the Module Name
Python is case-sensitive. The correct import uses all lowercase:
import Httpx
import HTTPX
import HttpX
import htpx
import httx
import httpxCommon httpx import patterns:
import httpx
from httpx import AsyncClient
from httpx import Client, AsyncClientSolution 3: Activate Your Virtual Environment
If httpx is installed inside a virtual environment, that environment must be active when you run your script:
source venv/bin/activate
venv\Scripts\activate
python -m pip show httpxIf httpx is not installed in the virtual environment:
pip install httpxSolution 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.")
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: 200Output (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 httpxCommon 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.0pyproject.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.txtConclusion
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.