How to Resolve ModuleNotFoundError No Module Named Dotenv in Python 3.8

The ModuleNotFoundError: No module named 'dotenv' occurs because python-dotenv is not installed in your active environment, or you are using Python 3.8 which is incompatible with modern versions of the Requests library that require Python 3.10 or newer.

When working with the psf/requests repository, developers often attempt to load API keys from .env files using python-dotenv. However, this package is not a dependency of Requests and must be installed separately. If you encounter the ModuleNotFoundError while running Python 3.8, the root cause is likely Requests' updated Python version requirements rather than a missing installation.

Why Python 3.8 Causes ModuleNotFoundError with Dotenv

The Requests library has dropped support for Python 3.8 in recent versions. In the pyproject.toml file located at the root of the psf/requests repository, the project declares:

requires-python = ">=3.10"

This metadata prevents the package from installing correctly on Python 3.8 interpreters. When you attempt to run code that imports both requests and dotenv under Python 3.8, the environment may fail to resolve dependencies correctly, leading to the misleading ModuleNotFoundError: No module named 'dotenv' even when python-dotenv appears to be installed.

Step-by-Step Resolution for ModuleNotFoundError

Follow these steps to eliminate the error and ensure compatibility between python-dotenv and the Requests library.

1. Upgrade to Python 3.10 or Newer

Create a new virtual environment using a supported Python version:

python3.10 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

This satisfies the requires-python = ">=3.10" constraint defined in Requests' pyproject.toml.

2. Install python-dotenv

With the correct Python version active, install the package:

pip install python-dotenv

This adds the dotenv module to your environment's site-packages, making it available for import.

3. Verify the Installation

Run a quick check to confirm the module resolves correctly:

import dotenv
print(dotenv.__version__)

If this executes without raising ModuleNotFoundError, your environment is correctly configured.

Complete Example: Using python-dotenv with Requests

Here is a practical implementation that loads environment variables before making HTTP requests:


# example.py

from dotenv import load_dotenv
import os
import requests

# Load variables from .env file

load_dotenv()

# Retrieve API key

api_key = os.getenv("API_KEY")

# Make authenticated request

response = requests.get(
    "https://api.example.com/data",
    headers={"Authorization": f"Bearer {api_key}"}
)

print(response.status_code)
print(response.json())

Execute the script with your activated virtual environment:

python example.py

Summary

  • The ModuleNotFoundError: No module named 'dotenv' indicates that python-dotenv is not installed in your active Python environment.
  • Requests requires Python 3.10 or newer as specified in pyproject.toml; using Python 3.8 causes compatibility issues that may mask the true import error.
  • Resolution requires upgrading to Python 3.10+, creating a fresh virtual environment, and running pip install python-dotenv.
  • Verify installation by importing dotenv in a Python shell before running your application code.

Frequently Asked Questions

Is python-dotenv included with the Requests library?

No. python-dotenv is a separate third-party package and is not listed as a dependency in the Requests pyproject.toml or setup.py files. You must install it manually using pip install python-dotenv.

Can I use python-dotenv with Python 3.8?

While older versions of python-dotenv (prior to 1.0) support Python 3.8, the Requests library itself no longer supports Python 3.8 as of recent versions. If you must use Python 3.8, you need to install an older version of Requests (2.27.x or earlier) and python-dotenv<1.0, though this is not recommended for security reasons.

Why do I still get ModuleNotFoundError after running pip install?

This typically occurs when pip installs the package into a different Python interpreter than the one running your script. Ensure you are using the same Python executable for both installation and execution: python -m pip install python-dotenv followed by python script.py, or activate your virtual environment before both commands.

How do I load .env files when using Requests for API calls?

Import load_dotenv from the dotenv module and call it before making HTTP requests. This reads the .env file into environment variables, which you can then access using os.getenv() to retrieve API keys or URLs for your requests.get() or requests.post() calls.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →