How to Use Pip with a Proxy Server
Try a faster and easier way to manage your Python dependencies. Use Python 3.9 by ActiveState and build your own runtime with the packages and dependencies you need. Get started for free by creating an account on the ActiveState Platform or logging in with your GitHub account.
By default pip installs third party packages from the Python Package Index (PyPI). In corporate environments located behind a firewall, you may require the use of an HTTP proxy server to redirect internal traffic to pypi.org, or any other repository that hosts Python packages.
Why Use a Pip Proxy Server?
A proxy server for pip is most commonly used for security and privacy, but can also be used for control:
- Security & Privacy – a proxy server can be used in combination with firewalls in order to improve internal network security since requests from users on the local network are anonymized via a proxy server.
- Control – organizations with an internal repository of Python packages may prefer to redirect any outbound traffic directed at pypi.org to their private repository instead, in order to prevent developers from installing unapproved packages.
For example, the ActiveState Platform provides the ActiveState Artifact Repository (AAR) that contains Python wheels built from source code. By using a proxy, you can ensure your developers are always working with secure, approved packages.
How to Use Pip with a Proxy Server
Pip supports basic HTTP-based authentication. This is done by embedding username/password login credentials in the URL.
Pip also supports command line completions in bash. To set up to use bash for proxy settings at a Linux command prompt, enter:
python -m pip completion --bash >> ~/.profile
To install Python packages via a proxy server, do the following:
Use the pip –proxy command option to specify a proxy in the following format:
sudo pip install --proxy http://<usr_name>:<password>@<proxyserver_name>:<port#> <pkg_name>
pip install --proxy http://<usr_name>:<password>@<proxyserver_name>:<port#> <pkg_name>
For proxies that only require a username:
pip install --proxy http://<usr_name>@<proxyserver_name>:<port#> <pkg_name>
To install a requirements.txt file via a proxy, enter:
pip install --proxy http://<usr_name>@<proxyserver_name>:<port#> requirements.txt
How to Use Pip With a Proxy Server in a Virtual Environment
When you have a proxy server between your virtual environment and Python package repository, you’ll need to set the proxy server environment variables:
# Append below text pattern at the end of ‘~/.bashrc’:
HTTP_PROXY=http://<usr_name>:<password>@<proxyserver_name>:<port#> Export HTTP_PROXY
Remember to export the variables after setting them, to make them available to the outer shell session.
# Add to environment variables: set HTTP_PROXY=<proxyserver_name>:<port#>
You can also modify the environment variable: PIP_USER_AGENT_USER_DATA to include a JSON-encoded string in the user-agent variable used in pip’s requests.
An alternative solution to Python security and package management – Try ActiveState’s Platform
Dependency resolution is at the core of the ActiveState Platform. When you create a project and start adding requirements, the Platforms tell you what dependencies those requirements have.
The ActiveState Platform is a cloud-based build tool for Python. It provides build automation and vulnerability remediation for:
- Python language cores, including Python 2.7 and Python 3.5+
- Python packages and their dependencies, including:
- Transitive dependencies (ie., dependencies of dependencies)
- Linked C and Fortran libraries, so you can build data science packages
- Operating system-level dependencies for Windows, Linux, and macOS
- Shared dependencies (ie., OpenSSL)
- Find, fix and automatically rebuild a secure version of Python packages like Django and environments in minutes
The ActiveState Platform aims to handle every dependency for every language. That means handling libraries down to the C/C++ level, external tools, and all the conditional dependencies that exist. To take things even further, our ultimate goal is to support multi-language projects. That means that you can create a project using both Python and Perl packages, and we’ll make sure that both languages are using the same (up to date) OpenSSL version.
Python Dependency Management In Action
Get a hands-on appreciation for how the ActiveState Platform can help you manage your dependencies for Python environments. Just run the following command to install Python 3.9 and our package manager, the State Tool:
powershell -Command "& $([scriptblock]::Create((New-Object Net.WebClient).DownloadString('https://platform.activestate.com/dl/cli/install.ps1'))) -activate-default ActiveState-Labs/Python-3.9Beta"
sh <(curl -q https://platform.activestate.com/dl/cli/install.sh) --activate-default ActiveState-Labs/Python-3.9Beta
Now you can run state install <packagename>. Learn more about how to use the State Tool to manage your Python environment.
Let us know your experience in the ActiveState Community forum.