Skip to content

Instantly share code, notes, and snippets.

@jtrive84
Last active March 29, 2023 09:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtrive84/48f0f8d3ca24db053b0daf4142fc8f8a to your computer and use it in GitHub Desktop.
Save jtrive84/48f0f8d3ca24db053b0daf4142fc8f8a to your computer and use it in GitHub Desktop.
How to download packages from pip behind a web proxy

Using pip behind a Web Proxy

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 & HTTPS_PROXY Environmental Variables

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 are unique to your login. To obtain the url and port used by the proxy server, open a command prompt and enter the following:

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

Alternatively, launch Internet Explorer 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:

$ HTTP_PROXY=http://username1:P@ssword1@proxy.corp.com:8080 

$ 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 PDF's:

$ 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

The above steps will also work when using the Anaconda distribution's package manager, conda. Set HTTP_PROXY and HTTPS_PROXY as above, and install packages as you normal using conda.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment