In this article, you’ll learn how to configure and use proxies with pip for seamless package management in restricted networks.
Differences Between Public and Private Proxies
When selecting a proxy, determining if it will be public or private is an important consideration.
Public Proxies
Public proxies are open for anyone to use and often lack authentication. While they can provide quick access to an IP address, they come with downsides—slower speeds, unstable connections, and potential IP bans. Because they are free and widely available, they often lack essential features like proxy rotations, caching, and access control, making them unreliable in a production environment.
A public URL may be formatted like this: https://proxyserver:port
.
Private Proxies
Private proxies require authentication, offering greater security, stability, and advanced features—though typically at a cost. They offer a secure, fast, and reliable connection to a dedicated IP address, along with features like proxy authentication and rotation.
Access is usually controlled through authentication often by including a username and password as a prefix to the proxy URL like this: https://username:password@proxyserver:port
.
Using Proxies with pip
To start using a proxy with pip, you need to gather some details about your proxy. The next example uses a public proxy with the following details:
- A proxy address for the proxy service
- The port the proxy service requires for communication
The following proxy-list
repo provides daily tested public proxy addresses that can be useful for testing but should not be used in production environments.
Within the proxy-list
repo, check the proxy-list-status.txt
file to find a working public proxy. This can be done by checking the file for an address that has the success
flag next to it, indicating that it is working:
For this tutorial, use 45.185.162.203:999
as your public proxy address. This means the proxy server address is http://45.185.162.203:999
.
Configuring a pip Proxy with the Command Line
The quickest way you can configure a pip proxy is to pass in the address when calling the pip install
command using the --proxy
command line option.
Using the public proxy address, test your access to the proxy and packages using the following command:
This method is helpful to quickly test and validate proxies before permanently configuring a new proxy. If publishing pip packages, it helps verify availability from a different IP.
Configuring a pip Proxy with the pip Config File
To permanently configure a pip proxy, the pip config file is an easy and declarative solution. Its location depends on your operating system and can be found in the following directories:
- Global: System-wide configuration file, shared across users.
- User: Per-user configuration file for the user executing the pip process.
- Site: Per-environment configuration file using Python virtual environments.
These configuration files can be found or created in the following locations for each system:
Linux/macOS
On Linux-based systems, the pip config file is called pip.conf
and can be found in the following locations:
- Global:
- Debian-based systems: Edit or create
pip.conf in the
/etc` directory. - macOS-based systems: Edit or create
/Library/Application Support/pip/pip.conf
.
- Debian-based systems: Edit or create
- User:
- Debian-based systems: Edit or create the
~/pip/pip.conf
file. - macOS-based systems: Edit or create the
~/.config/pip/pip.conf
config file.
- Debian-based systems: Edit or create the
- Site: When loaded in a Python virtual environment, it’s located at
$VIRTUAL_ENV/pip.conf
.
Windows
On a Windows system, the file is a pip.ini
file and can be found in the following locations:
- Global: Edit or create the
C:\ProgramData\pip\pip.ini
file. Note that this file is hidden by default on Windows systems but is writable. - User: Edit or create
pip.ini
in%APPDATA%\pip\
. - Site: When loaded in a Python virtual environment, edit or create the config file at
%VIRTUAL_ENV%\pip.ini
.
Config File Contents
For this example, a Python virtual environment pip config file is used. In an activated virtual environment, edit $VIRTUAL_ENV/pip.conf
on Debian-based systems or %VIRTUAL_ENV%\pip.ini
on Windows.
In the config file, you need to update the proxy
key with your desired HTTP or HTTPS proxy:
Once the file is saved, the proxy is used automatically with any pip command, eliminating the need for the proxy flag mentioned previously:
You can find more details on the configuration options available in the pip config file in the project’s documentation.
Configuring a pip Proxy with Environment Variables
Setting system environment variables ensures that a proxy is used for pip and all other HTTP requests on a system. This is done by utilizing both the HTTP_PROXY
and HTTPS_PROXY
environment variables, which are often referenced by software like pip as a system proxy to use when making HTTP requests.
Linux/macOS
If you’re using a Linux operating system, update the /etc/environment
file, or if you’re a macOS user, update the .zshrc
file located in the home directory. Then, update it with new entries for your proxy server:
Once you restart your terminal sessions or restart your system, the environment variables will be present.
Windows
On a Windows system, you can set environment variables with the following commands in a command prompt terminal:
Restart the command prompt for the changes to take effect.
Testing the Configuration
Once you have a system-level configuration enabled either through the pip config file or environment variables, you should test that the proxy can successfully connect and receive data via the proxy.
Linux/macOS
On Linux/macOS, use the following command:
If you ever want to override these settings with a specific proxy, you can fall back on using the CLI flags:
In this command, make sure you update https://proxyserver:port
with your own proxy.
Windows
On Windows, use the following command:
These settings can always be overridden using the pip CLI flags:
Troubleshooting pip Proxies
When connecting to an HTTP or HTTPS proxy with pip, you may come across the following common issues, particularly if you explore using private proxies or HTTPS proxies due to their enhanced features.
Authentication Issues
Authentication issues are commonly seen as a 407 Proxy Authentication Required
error when trying to connect to the proxy with pip. This indicates that the proxy requires a username and password to connect or you have provided the wrong credentials for the proxy.
Certificate Issues
When connecting to an HTTPS proxy, you may receive a Certificate verify failed
error from pip. This indicates that there is an issue with the certificate provided by the proxy server.
If your private proxy server is using a self-signed certificate, you may get this error, and the certificate cannot be verified with a certificate authority. You may want to use the --trusted-host
CLI option when connecting to certain domains and ignore self-signed certificate errors.
Using pip with Rotating Proxies
Rotating proxies help avoid IP bans by automatically switching IP addresses for each request. This mimics multiple users and bypasses restrictions.
You can implement this by randomly selecting proxies from a list. Below is a simple bash script that installs pip packages while rotating through public proxies.
Create the following bash script called rotate-proxies.sh
:
Once created, you can then execute the file to download the pip packages and rotate through a random proxy for each pip command. Here’s a summary of the script’s output:
Benefits of Using Proxies with pip
Proxies help developers bypass network restrictions, access blocked resources, and improve package download speeds. Private proxies offer caching, faster connections, and enhanced security by masking your identity.
Compared to a VPN , proxies are a lightweight alternative for pip requests. While a VPN encrypts all internet traffic for broader privacy, it can slow down package installations due to increased latency. Proxies provide a faster, more efficient solution for managing dependencies.
Common Mistakes and Best Practices
When using proxies with pip, it’s important to watch out for common mistakes as they can lead to security vulnerabilities. Errors such as incorrect proxy URLs or misconfigured URL formats—like missing or incorrect HTTP or HTTPS protocol—can disrupt connections to package repositories.
A frequent security issue is hard-coding proxy credentials (eg usernames and passwords) in source codes, scripts, or continuous integration, continuous delivery (CI/CD) pipeline definitions. If this code is redistributed, unauthorized access to the proxy can occur. If credentials are compromised, the proxy may be misused, leading to increased costs or even exploited by cyberattacks.
To avoid these mistakes, it’s recommended that proxy credentials are kept secure by storing them in environment variables or encrypted configuration files rather than directly in code. Additionally, you must test proxy connectivity before using pip to ensure proper setup and avoid runtime errors. Using tools like curl or ping, you can verify proxy performance before putting it into service. This enables a smoother package management experience.
Using Bright Data Proxies
If you’re looking for a provider of high-quality and featured proxies, check out Bright Data. It’s a proxy solution that provides a range of IP addresses, including residential, data centers, and mobile devices. It also provides specialized tools for data collection and web scraping, including rotating proxies and the Web Unlocker API.
Bright Data can help you easily create proxies for your project’s needs. To show you how easy it is, let’s create a private residential proxy that you can use with pip to access packages via a different IP address.
Start by signing up for a free Bright Data account. Then navigate to the user dashboard.
On the side menu, click on Proxies & Scraping:
Once the form has loaded, configure a new residential proxy. If you use the default settings, you’ll get a proxy with a shared IP address used by multiple Bright Data users:
If you’re targeting a specific region, you can also specify the country in which you want the IP address to be located.
After creation, you are redirected to a dashboard that contains the endpoint and authentication details for your newly created proxy. Take note of both the username, password, and server address:
Using these endpoint values, test their availability using the --proxy
flag:
Because the Bright Data proxy uses a self-signed certificate, you can use the trusted-host
flag to whitelist pypi.org
and files.pythonhosted.org
as trusted domains.
Conclusion
Using a proxy with pip helps enhance anonymity and control, enabling you to bypass IP restrictions and geoblocking. Depending on your needs, you can choose between public and private proxies.
Private proxies offer advantages, like rotating IPs, improved caching, faster speeds, and greater stability, but the cost depends on IP availability and geographic location.
Configuring a proxy for pip is simple, with multiple options like CLI flags, a pip config file, and environment variables. However, public proxies have limitations, and as discussed previously, they aren’t ideal for large workloads or production use. For a more reliable solution, Bright Data offers residential and datacenter IPs, providing fast, stable connections and advanced tools for web scraping and data collection. Sign up for free to get started.
No credit card required