Skip to main content

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:

  1. httpx is not installed in the current Python environment.
  2. Incorrect module name: a typo or wrong casing in the import statement.
  3. 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
Virtual environments are isolated

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​

  1. Press Ctrl+Shift+P (or Cmd+Shift+P on macOS).
  2. Type "Python: Select Interpreter".
  3. Choose the interpreter or virtual environment where httpx is installed.

PyCharm​

  1. Go to File → Settings → Project → Python Interpreter.
  2. Verify the selected interpreter has httpx listed.
  3. 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​

ScenarioDiagnosticFix
httpx not installedpip show httpx shows nothingpython -m pip install httpx
Typo in importImport says Httpx or htpxChange to import httpx
Wrong virtual environmentwhich python points to wrong pathActivate the correct venv
IDE using wrong interpreterCheck IDE interpreter settingsSelect the correct interpreter in IDE settings
Multiple Python versionspython --version vs python3 --versionUse python3 -m pip install httpx
Docker / CI environmentPackage not in requirements.txtAdd 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.