17

I'd like to install a certain python package with pip but because of the proxy I am sitting behind pip cannot connect to the internet.

So my question is: Where does pip look for .whl files in order to download them? Can't I just use my browser (which can connect to the internet just fine) to download the .whl file? Installing the package with the downloaded .whl file would be not a problem then.

elzell
  • 2,228
  • 1
  • 16
  • 26
  • https://pypi.python.org/pypi – Mad Physicist Jan 18 '18 at 17:26
  • Why not just configure your proxy? – Martijn Pieters Jan 18 '18 at 17:27
  • @MartijnPieters It's the proxy of our company network which takes username and password. I've managed to configure it in the past with set HTTP_PROXY="username:password@proxy..." in the windows shell but for some reason this doesn't work anymore. – elzell Jan 18 '18 at 17:30
  • @elzell: `pip --proxy username:password@proxy... install ...` should work. – Martijn Pieters Jan 18 '18 at 17:31
  • @MartijnPieters Unfortunately it doesn't. I guess it's the proxy because only some of the software on my computer which can make an internet connection (e.g. for updates) works when I set the proxy settings correctly - also Firefox only works when I set "Detect proxy settings automatically". That's why I didn't want to struggle with it anymore and look for an easier way. – elzell Jan 18 '18 at 17:36
  • 2
    Why not ask your IT department how to configure the proxy? Pip supports most proxy scenarios, it’s just another HTTPS client. – Martijn Pieters Jan 18 '18 at 17:40
  • @MartijnPieters Sure I will do this, but that means opening a ticket and waiting some days or weeks before I get a reply, so I had hoped for the expertise of Stackoverflow to get a quick solution. – elzell Jan 18 '18 at 17:48
  • More relevant today now that pip doesn't work in python < 2.7.9 – Jonathan Rys Jul 15 '21 at 20:31

2 Answers2

14

pip searches the Python package index (PyPI), each package lists downloads (including wheels, if there are any) with a direct download link on the page. Package pages have the form of https://pypi.python.org/pypi/<package_name> or https://pypi.python.org/pypi/<package_name>/<version> for specific versions.

If you can only download wheels manually with your browser, it doesn't matter where you put the wheel file. Just install the wheel file directly:

pip install path/to/wheel.whl

However, pip supports downloading over a proxy just fine:

pip --proxy username:password@proxy_server:proxy_port install ...

See the --proxy command line switch documentation. You can add the proxy setting to a pip configuration file so you don't have to set it on the command line each time, or by setting environment variables; see the Using a Proxy Server section in the Pip User Guide.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • In 2021 this answer is only relevant to Python 2.7.9+ – Jonathan Rys Jul 15 '21 at 20:32
  • @JonathanRys: in what way? Python 2.7.9 is hardly relevant any more, or are you saying this doesn't apply to Python 3.x? I can assure you that my answer is as relevant today as it was 3 years ago. – Martijn Pieters Jul 16 '21 at 20:04
  • Your comment is relevant today to supported versions of Python, but this is no longer true for Python < 2.7.9 because PyPi has dropped non-SNI support across the board. That means pip and get-pip no longer work there. – Jonathan Rys Jul 16 '21 at 22:24
  • @JonathanRys yes, or rather, Fastly, the CDN provider, is dropping support and so PyPI must follow. I’ve kept a few backports to older Python releases going for a while but even if you can’t migrate away from Python 2 at this point, you should not ever be using anything less than Python 2.7.9+ for several other reasons, not least due to security vulnerabilities. – Martijn Pieters Jul 18 '21 at 10:20
8

How to get an URL pip is using to download the file:

  • Get JSON from https://pypi.python.org/pypi/package_name/json
  • parse releases part, select the latest release
  • go through available files (usually there are more than one), taking your platform into account (e.g. x32 vs x64, Windows or Linux version, installed Python etc)
  • use url property

E.g.:

import requests
package = requests.get("https://pypi.python.org/pypi/pandas/json").json()
max_ver = max(package["releases"].keys())
# ... check compatibility
file = get_file_idx(package['releases'][max_ver])
urllib.urlretrieve(file["url"])
Marat
  • 15,215
  • 2
  • 39
  • 48
  • Showing the `requests` path is all very well, but rather pointless when the OP is having issues configuring a proxy, don't you think? A *human* would just go to the package page, not the JSON URL. – Martijn Pieters Jan 18 '18 at 17:43
  • @MartijnPieters I understand this is not the answer to the original question, but I imagine this snippet might come in handy in a similar situation – Marat Jan 18 '18 at 17:53