pip is a package management system used primarily to install and maintain third-party libraries written for Python. pip will not work in an enterprise setting behind a web proxy without first setting environmental variables specifying the user's authentication details, in addition to the url and port of the proxy server. This post walks through how to setup and use pip from behind a web proxy on Windows.

Setting HTTP_PROXY and HTTPS_PROXY

The first step is to set two environmental variables from the command line: HTTP_PROXY and HTTPS_PROXY. The required format is:

HTTP_PROXY=http://[username]:[password]@[proxy_address]:[port_number]
HTTPS_PROXY=http://[username]:[password]@[proxy_address]:[port_number]

The username and password correspond to your login. To obtain the url and port used by the proxy server, open a command prompt and enter the following:

C:\> reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings" | find /i "proxyserver"

Alternatively, launch Edge and navigate to Tools > Internet Options > Connections > LAN Settings, then under Proxy Server, note your address and port.

Assume the username and password are username1 and P@ssword1, and the result of the wmic query returned proxy.corp.com and port 8080. From the command line, run:

C:\> set HTTP_PROXY=http://username1:P@ssword1@proxy.corp.com:8080 
C:\> set HTTPS_PROXY=https://username1:P@ssword1@proxy.corp.com:8080

With regard to storing authentication details in an environmental variable: The method we utilized in defining HTTP_PROXY and HTTPS_PROXY limits the variables lifetime to the duration of the terminal session. As soon as you close the command prompt, HTTP_PROXY and HTTPS_PROXY will no longer be defined. In general, there's no need to encrypt the contents of an environment variable. The reason you can see HTTP_PROXY and HTTPS_PROXY is because your user account has the necessary privileges. If you attempted logging in as a different user you'd have no way to access the environmental variables associated with your account.

Finally, we call pip from the command prompt. Lets install the PyPDF2 package, a third-party library that's useful for manipulating PDFs:

` C:\> pip install PyPDF2 Collecting PyPDF2 Downloading PyPDF2-1.26.0.tar.gz (77kB) 100% |################################| 81kB 436kB/s Building wheels for collected packages: PyPDF2 Running setup.py bdist_wheel for PyPDF2 ... done Stored in directory: C:\Users\Opticks\AppData\Local\pip\Cache\wheels\... Successfully built PyPDF2 Installing collected packages: PyPDF2 Successfully installed PyPDF2-1.26.0