Configure Python Requests Proxy via .env | Generated by AI

Home 2025.10

Using a .env File to Set HTTP/HTTPS Proxies for Python Requests

The requests library in Python automatically honors standard environment variables like HTTP_PROXY and HTTPS_PROXY for proxy configuration. To manage these securely (e.g., without hardcoding in your script), you can store them in a .env file and load them using the python-dotenv library. This keeps sensitive proxy details out of your code.

Step 1: Install Required Packages

You’ll need requests (if not already installed) and python-dotenv for loading the .env file.

pip install requests python-dotenv

Step 2: Create a .env File

In your project root, create a file named .env (no extension) and add your proxy settings. Use the format http:// or https:// for the proxy URL, including username/password if required.

Example .env content:

HTTP_PROXY=http://username:password@proxy-host:port
HTTPS_PROXY=https://username:password@proxy-host:port
NO_PROXY=localhost,127.0.0.1,example.com  # Optional: Exclude domains from proxying

Add .env to your .gitignore to avoid committing sensitive info.

Step 3: Load the .env File in Your Python Script

At the top of your script, load the environment variables:

from dotenv import load_dotenv
import requests

# Load variables from .env file
load_dotenv()  # Looks for .env in the current directory by default

# Now make a request – proxies are automatically applied
response = requests.get('https://httpbin.org/ip')
print(response.json())

Step 4: Verify Proxy Usage (Optional)

To confirm proxies are working, test with a service like httpbin.org:

import requests
from dotenv import load_dotenv

load_dotenv()

proxies = requests.utils.get_environ_proxies('https://httpbin.org/ip')  # Inspect loaded proxies
print("Loaded proxies:", proxies)

response = requests.get('https://httpbin.org/ip')
print("Your IP via proxy:", response.json())

This should show the proxy server’s IP if configured correctly.

Common Issues and Tips

This setup is simple, secure, and follows best practices for environment-based configuration.

Requests Documentation: Proxies
python-dotenv Documentation ```


Back

x-ai/grok-4-fast

Donate