How to configure PIP per config file to use a proxy (with authentification)?
Asked Answered
U

5

35

I used to set up environment evariables http_proxy and https_proxy (with user + password) in the past to use Pip (on Windows) behind a corporate proxy. But recently I needed to tell Pip to use a proxy without setting up environment variables as this conflicted with git configuration in combination with SSL Certificates which I get to work only by removing environment variables for proxy.

Fortunately you can configure PIP with an pip.ini file as described here: https://pip.pypa.io/en/stable/user_guide/#config-file

The detailed answer to my own question follows below.

Uke answered 18/4, 2017 at 13:4 Comment(0)
U
57

Here are the steps how to configure proxy (with auth.) in pip's config file (pip.ini)

  1. (if it does not already exist) Create a folder named 'pip' and inside it a file named 'pip.ini' as described here: https://pip.pypa.io/en/stable/user_guide/#config-file (location an name may differ per platform - e.g. on Windows it's %APPDATA%\pip\pip.ini)
  2. edit pip.ini file and add

    [global]
    proxy = http://user:password@proxy_name:port
    
  3. That's it!

Example for proxy with authentification (user + password):

proxy = http://butch:secret@proxyname:1234

proxyname can be an IP adress, too

Example for proxy without auth.:

proxy = http://proxyname:1234

Uke answered 18/4, 2017 at 13:14 Comment(6)
Thanks, this solved my problem... I didn't need username or password, just the proxy IP.Bruis
In cmd, do I just need to write "pip install requests" for example? Will the ini be automatically checked? I can't seem to get it working.Phosgene
In Windows 10, the path will be: C:\Users\<username>\AppData\Roaming\pip\pip.iniInheritor
this works to me but naming the conf file pip.conf instead of pip.ini as the docs say (using ubuntu 18.04)Seduction
exactly, what does the pip install... command look like now after setting up this configuration file?Swallow
@Swallow pip install ... will not change. e.g. pip install robotframework. If u do not setup a pip.conf (or pip.ini) you will have to provide proxy info as cli option whenever you call pip (see the other answers below), e.g. pip install robotframework --proxy https://user_name:password@proxyname:portUke
T
32

A little easier with:

pip config set global.proxy http://{host}:{port}

and it will persist the setting automagically

Writing to C:\Users\{username}\AppData\Roaming\pip\pip.ini
Tuner answered 14/10, 2021 at 10:12 Comment(1)
I have used this after activating my virtualenv with pyenv. A configuration has been created just for this environment and it solved my issue with corp proxy. Thank you.Hindi
J
14

In order to add a proxy option in the terminal the following line solved the problem for me:

pip install package_name_here --proxy https://user_name:password@proxyname:port
Jahn answered 30/10, 2018 at 9:33 Comment(0)
A
4

You need to set proxy option while installing the package. example:

pip install --proxy userid:[email protected]:yourport
Assiduous answered 21/6, 2018 at 12:44 Comment(0)
H
2

If package that you are trying to install has dependencies it's best to create pip.ini for system wide configuration, in windows you can do this in powershell:

mkdir c:\programdata\pip\
new-item c:\programdata\pip\pip.ini

and add this to your pip.ini

[global]
proxy = http://domain\user:pwd@proxy_hostname_or_ip:port 

and then everything should work fine, as HTTP_PROXY variable didn't work for me.

Make sure to save file as ansi or windows1252 in VSCode as UTF files are not read properly.

Hypesthesia answered 14/7, 2020 at 17:10 Comment(1)
Pip uses variables with prefix PIP_. The rest is the option name in uppercase with underscores. On Unix: export PIP_PROXY=... on Windows: set PIP_PROXY=... --- See pip.pypa.io/en/stable/user_guide/#environment-variablesSimone

© 2022 - 2024 — McMap. All rights reserved.