Proxying pip
// How to install Python packages with pip when you're behind a company firewall.
If youâre working with python on your companyâs network, chances are youâre behind a firewall and that you may have run into issues whilst trying to install python packages with pip. This is because pip doesnât know how to use your proxy. Maybe this will help.
Whatâs a proxy?
A proxy is a server that sits between your computer and the internet. Itâs typically used to filter requests and block certain websites.
Getting around it
You can tell pip that it needs to work with your proxy by setting the http_proxy
and https_proxy
environment variables. Alternatively, you can use the --proxy
flag when running a pip command (Iâve personally had a bit more success with this method).
Constructing the proxy url
Assuming youâre on a windows machine, you can find the proxy url by searching for internet options
in the start menu. Then go to the Connections
tab and click on LAN settings
. You should find the proxy url in the Proxy server
section. Alternatively you can go to Settings
and search for proxy
.
The proxy url should look something like this: http://proxy.company.net
. The part we care about is the proxy.company.net
. Typically 8080
is the default port, but your company may use a different port.
Once you have this url, you can them construct the url that we will pass to pip. It should look something like this:
http://<username>:<uri_encoded_password>@proxy.company.net:8080
The <username>
and <uri_encoded_password>
are your windows credentials.
The <uri_encoded_password>
is the URI encoded version of your password where any special characters are replaced with their % encoded counterparts e.g. password!
becomes password%21
. Here are the commonly used characters and their encoded counterparts:
Character | Encoded Character |
---|---|
! | %21 |
@ | %40 |
# | %23 |
$ | %24 |
% | %25 |
^ | %5E |
& | %26 |
* | %2A |
You can find a more comprehensive list of characters here.
Installing packages
Once you have constructed the proxy url, you can pass it to pip using the --proxy
flag. Hereâs an example:
pip install -r requirements.txt --proxy http://<username>:<uri_encoded_password>@<proxy_url>:<port>
Thanks for reading! đ«¶
I would love to hear your thoughts on this. If you have any questions or comments, please feel free to reach out on any of the platforms below. I look forward to connecting with you!