Tech

How to Use “pip install” With Proxy (Full Guide)

If you’ve ever tried to run a pip install and ended up stuck with connection errors, you’re not the

How to Use “pip install” With Proxy (Full Guide)

If you’ve ever tried to run a pip install and ended up stuck with connection errors, you’re not the only one. Errors usually happen when you’re in a company network , school network or behind a firewall that blocks direct internet access. pip needs to download packages from the internet, so when that’s blocked, the installs fail.

In this guide, I’m going to show you how to get pip install working with a proxy step by step. You’ll learn why pip needs a proxy, how to pass the proxy details into the command line, how to set them up, and how to fix the most common errors that come up.

Why pip Sometimes Requires a Proxy

pip downloads Python packages from public servers like PyPI (Python Package Index). On a typical home network, it happens quietly in the background. But on restricted networks, pip can’t get online without a fight. It can’t connect directly to the internet.

This is a pretty common issue in places such as:

  • Company offices
  • Schools or universities with strict filter rules
  • Cloud servers with security firewalls

In these environments, all internet traffic has to go through a proxy server, essentially the gatekeeper to the internet. If pip doesn’t know about it and tries to get through, the request gets blocked.

When that happens, you’re usually met with some errors, such as:

  • Connection timed out
  • Failed to establish a new connection
  • ProxyError or Cannot connect to host pypi.org

Once you tell pip which proxy to use, it routes the downloads through that proxy server

CLI Example for pip install with Proxy

When you give the proxy details directly in the command line, pip will start working with the proxy right away. This method is great for installing something just once and not changing any settings on the computer.

The basic format:

pip install package-name –proxy protocol://proxy_ip:port

An example:

pip install requests –proxy http://127.0.0.1:8080

If your proxy needs a username and password, do it like this:

pip install numpy –proxy http://username:password@proxy_ip:port

Full example:

pip install pandas –proxy http://john:pass123@10.0.0.2:3128

Because the proxy is only used for that one command, this method is frequently called pip install with proxy. Pip returns to its default behavior after the installation is complete.

Setting Environment Variables

Setting environment variables is a more convenient strategy to avoid having to type the proxy address every time. Once set up, pip will use the proxy without additional flags for each install.

On Windows

Open Command Prompt and run:

set HTTP_PROXY=http://username:password@proxy_ip:port

set HTTPS_PROXY=http://username:password@proxy_ip:port

If your proxy doesn’t require login details:

set HTTP_PROXY=http://proxy_ip:port

set HTTPS_PROXY=http://proxy_ip:port

On macOS and Linux

In the terminal, run:

export HTTP_PROXY=http://username:password@proxy_ip:port

export HTTPS_PROXY=http://username:password@proxy_ip:port

Without authentication:

export HTTP_PROXY=http://proxy_ip:port

export HTTPS_PROXY=http://proxy_ip:port

Using pip.ini / config Files

Using a pip config file is the best choice if you want a clean, durable setup. After this is set up, pip will always use the proxy – no flags, no environment variables, and no additional steps.

Where to find the config file

The file name and location depend on your system:

Windows:

 %APPDATA%\pip\pip.ini

macOS & Linux:

 ~/.config/pip/pip.conf

If the file or folder doesn’t exist, you can create it.

What to put inside the file

Open the file and add:

[global]

proxy = http://username:password@proxy_ip:port

If your proxy doesn’t need authentication:

[global]

proxy = http://proxy_ip:port

All you need to do is save the file. The proxy will be used automatically in the background for all pip install commands.

Troubleshooting

Don’t panic immediately if pip install still fails after configuring your proxy. Most of the time, the issue is simply a small setup error. These are the most common ones to look at.

Proxy Authentication Errors

If you’re getting errors that are related to authorising with the proxy, take a look at:

  • Your username and password – check that they’re correct
  • Any special characters in your password – make sure they’re properly URL encoded
  • Whether your proxy even supports authentication

HTTPS and SSL Troubles

If you’re seeing errors like SSL: CERTIFICATE_VERIFY_FAILED , that usually means your proxy is trying to inspect your HTTPS traffic.

  • Double check that ‘HTTP_PROXY’ and ‘HTTPS_PROXY’ are both set
  • Try using ‘http://’ instead of ‘https://’ in your proxy URL
  • As a last resort (and not recommended in a production environment), you could try testing with this – pip install package-name –trusted-host pypi.org –trusted-host files.pythonhosted.org

Connection Timeouts

If pip just hangs or times out:

  • Check your proxy IP and port are correctly set
  • Make sure the proxy is allowing traffic to the pypi.org
  • Test your proxy in a browser or with curl to make sure it actually works

Proxy Works in Browser but Not in pip

This often happens when the browser is using the OS’s built-in proxy settings, but pip is just following its own config or environment variables. Make sure that your pip proxy settings are set specifically for pip and not just for the OS.

Wrapping Up

If pip install causes issues on your network, it’s usually caused by a common issue: pip is unable to establish an independent internet connection. By giving pip a direct path to download the necessary packages, a proxy brings it back online.

The good news is that the outcome is the same whether you enter the proxy information in the command line, use environment variables, or store it in a pip config file. Installations function as intended once pip is aware of where to send its requests.

The majority of the time, errors are not related to pip itself, but rather result from a minor error in the proxy details or perhaps a change to the way it handles SSL.

About Author

Felix Pembroke

Felix Pembroke is a UK-based tech enthusiast and the author behind TechImaging.co.uk. With a keen eye for innovation and a passion for simplifying complex technology, Felix provides insightful content that helps readers stay informed and ahead of the tech curve. Felix also covers general content, making TechImaging.co.uk a versatile source for a broad range of topics.

Leave a Reply