How to Set pip Proxy Settings Using Environment Variables

Working behind a firewall, corporate network, or restricted ISP can make it difficult to install Python packages using pip. Some of the signs that pip cannot establish a direct connection to the internet are connection timed out or unable to fetch URL.
When this happens, a proxy server connects pip to external package repositories such as PyPI.
I’ll explain how pip proxy settings work, how to set them up on various operating systems, and how to ensure they function properly in this guide.
Why pip Needs Proxy Configuration
pip downloads packages over the internet, typically from the Python Package Index (PyPI). Sometimes, direct internet access is restricted. This is common in data centers, corporate offices, educational institutions, and some cloud environments.
When a network prevents outbound connections, Pip won’t automatically establish a connection to PyPI. Errors such as failed to fetch URL, temporary name resolution failure, or connection timeout may occur in the absence of a proxy.
A proxy server that is set up to route pip traffic via an approved network path takes care of this. Once configured, pip will send all download requests via the proxy rather than establishing a direct connection. This makes it possible for installation, updates, and dependency downloads to continue as usual, even in restricted environments.
How pip Uses Environment Variables for Proxy Settings
Pip scans your system to determine how to connect to the internet. Pip will use proxy variables without additional configuration files or flags once they are set up. Because of this, using environment variables to run pip with proxy server access across multiple projects and scripts is the cleanest method.
Pip searches the following key variables:
- HTTP_PROXY or http_proxy
- HTTPS_PROXY or https_proxy
If these variables are present, all HTTP and HTTPS traffic is redirected to the proxy address. This is relevant to:
- Downloads of packages from PyPI
- Resolving dependencies
- Updates and version checks
Setting pip Proxy via Environment Variables on Windows
On Windows, it can be set temporarily (per session) or permanently (system-wide). Pip works with both of them.
Temporary (Command Prompt or PowerShell)
This technique applies only to the active terminal session.
Command Prompt:
set HTTP_PROXY=http://proxy-ip:port
set HTTPS_PROXY=http://proxy-ip:port
PowerShell:
$env:HTTP_PROXY=”http://proxy-ip:port”
$env:HTTPS_PROXY=”http://proxy-ip:port”
After that, execute your pip command as usual:
pip install requests
Permanent (System-Wide)
- Access System Properties
- Select Environment Variables.
- Click New under User variables or System variables.
- Add:
- Variable name: HTTP_PROXY
- Variable value: http://proxy-ip:port
- Repeat for HTTPS_PROXY
- Restart your terminal
Setting pip Proxy via Environment Variables on macOS and Linux
Proxy settings are usually included in the terminal or shell configuration files on Linux and macOS. It is easy to set up and can be used again in different sessions.
Temporary (Current Terminal Session)
To configure the proxy for the current session only, use the export command:
export HTTP_PROXY=http://proxy-ip:port
export HTTPS_PROXY=http://proxy-ip:port
Run pip the usual way after that:
pip install numpy
Permanent (Recommended)
Add these parameters to your shell configuration file to make the proxy persistent:
- Bash: ~/.bashrc or ~/.bash_profile
- Zsh (default on macOS): ~/.zshrc
Add:
export HTTP_PROXY=http://proxy-ip:port
export HTTPS_PROXY=http://proxy-ip:port
Save the file and reload it:
source ~/.zshrc
Authenticating Proxy Connections (Username & Password)
Some proxies need authentication. In that case, the proxy URL requires a username and password.
Basic Format
http://username:password@proxy-ip:port
Example (Environment Variables)
Windows (PowerShell):
$env:HTTP_PROXY=”http://user:pass@proxy-ip:port”
$env:HTTPS_PROXY=”http://user:pass@proxy-ip:port”
macOS / Linux:
export HTTP_PROXY=http://user:pass@proxy-ip:port
export HTTPS_PROXY=http://user:pass@proxy-ip:port
Important Notes
- Your password needs to be URL-encoded if it contains any special characters (@, #, %, :). – For example, p@ss:word → p%40ss%3Aword
- Don’t include proxy credentials in scripts or public repositories.
- For security reasons, prefer environment variables at the user level over those at the system level.
Verifying That pip Uses the Proxy Correctly
It’s important to make sure that pip is really using the proxy after setting it. By doing this, confusing network errors and silent failures can be avoided.
Run pip in Verbose Mode
Use the -v flag to see connection details:
pip install requests -v
If the proxy is working, you should see lines showing connections being made through the proxy address instead of directly to PyPI.
Test with a Simple Package
Install a small, commonly used package:
pip install wheel
If the installation completes without timeout or connection errors, the proxy is configured correctly.
Check Environment Variables
Verify that the variables are set:
Windows:
echo %HTTP_PROXY%
macOS / Linux:
echo $HTTP_PROXY
Pip will use the correct proxy URL automatically if it appears.
Common Errors When Using pip Behind a Proxy
Connectivity can be disrupted even by small mistakes in pip setup. The most common proxy related issues and their definitions are below.
- Connection timed out – The proxy IP or port is not accurate, unavailable or blocked by the firewall.
- 407 Proxy Authentication Required – The proxy requires a username and password, which are invalid or not provided.
- Unable to connect to URL /SSL error – The proxy either intercepts the connection to the SSL or does not support HTTPS traffic.
- Name resolution error – The proxy does not allow domain lookups or blocks the DNS.
- Proxy settings ignored by pip – The terminal was not re-executed, the environment variables were not exported or both.
- The special characters cause password failures – The proxy credentials were not coded with a URL.
- Browsing works, pip does not – The browser uses system proxy settings, whereas pip uses only environment variables.
Ti potrebbe interessare:
Segui guruhitech su:
- Google News: bit.ly/gurugooglenews
- Telegram: t.me/guruhitech
- Facebook: facebook.com/guruhitechfb
- Instagram: instagram.com/guruhitech_official/
- X (Twitter): x.com/guruhitech1
- Bluesky: bsky.app/profile/guruhitech.bsky.social
- Rumble: rumble.com/user/guruhitech
- VKontakte: vk.com/guruhitech
- MeWe: mewe.com/i/guruhitech
- Skype: live:.cid.d4cf3836b772da8a
- WhatsApp: bit.ly/whatsappguruhitech
Esprimi il tuo parere!
Ti è stato utile questo articolo? Lascia un commento nell’apposita sezione che trovi più in basso e se ti va, iscriviti alla newsletter.
Per qualsiasi domanda, informazione o assistenza nel mondo della tecnologia, puoi inviare una email all’indirizzo [email protected].
Scopri di piรน da GuruHiTech
Abbonati per ricevere gli ultimi articoli inviati alla tua e-mail.
