Search code examples
pythonpip

How to install PIP packages through a proxy?


I have RHEL 8 server which has not internet connection and the server has a jupyter notebook installed. I need to install exchangelib module there. Since the server has no any internet connection I couldn't do that. So I started to create a proxy like below.

http_proxy  = "http://10.11.111.11:3128"
https_proxy = "https://10.11.111.11:3128"
ftp_proxy   = "ftp://10.11.111.11:3128"

proxyDict = { 
              "http"  : http_proxy, 
              "https" : https_proxy, 
              "ftp"   : ftp_proxy
            }

# setting up the URL and checking the connection by printing the status

url = 'https://www.google.lk'
page = requests.get(url, proxies=proxyDict)
print(page.status_code)
print(page.url) 

The output of the following code is as follows.

200
https://www.google.lk

So I was able to connect to internet by using that. But I couldn't figure out how I install pip packages after that. Can anyone guide me on that?


Solution

  • You shouldn't use pip as a library. pip project recommends using it with subprocess call.

    subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'my_package'])
    

    Then for proxy you can add the --proxy flag to it. This Stackoverflow answer shows it well. But to complete the answer, this is how it should look like,

    subprocess.check_call([
        sys.executable, 
        '-m',
        'pip',
        'install',
        '--proxy',
        'http://10.11.111.11:3128',
        'my_package'
    ])