Introduction to python setting up proxy and adding image source

Why modify the mirror source?

Generally, the python installation library is used, and the pip install xxx instruction or conda install xxx instruction is used. Because pip and conda are the default foreign image sources, they will be in Python's official source, pypi.python.org/pypi Download is very slow. Sometimes it will throw exceptions and fail to download successfully due to timeout. Therefore, you can choose some domestic images with relatively stable and fast speed to download Python library, which can significantly speed up module installation.

Why set up a proxy?

When the computer is limited to access the Internet (company security considerations), it can't connect to the Internet. At this time, it needs to set up an agent to solve the problem. Usually, the company requires to connect to the Internet through an agent to ensure the network security (that is, when the network connects to the Internet, an agent needs to be set up to access); sometimes because the pypi of the foreign official is blocked, it is not available, or it can be accessed through setting an agent.

[note]: pip and conda modify the image source in different ways

configuration file

Before setting the agent and adding the image source, check whether the configuration file exists. If not, create it. Path to profile (different system paths are different):

1). Unix: $HOME/.config/pip/pip.conf

2). Mac: $HOME/Library/Application Support/pip/pip.conf

3). Windows:% APPDATA%\pip\pip.ini,% APPDATA% indicates the actual path, for example, C: \ users \ user XXX \ APPDATA \ roaming, you can execute the echo% APPDATA% command in cmd to view

[supplement]:

1) . all pip command line parameters can be defined as default values in the configuration file!

2). which pip can indicate the location of pip (for example, / usr/local/bin/pip)

pip set up proxy

1) . single setting: (same for Windows and Linux systems)

Set the proxy directly in pip: pip install some package -- proxy = http: / / proxy server IP: port number

1 pip install pandas --proxy=http://10.20.217.2:8080

2) . temporary settings (invalid after reconnection):

(1) under Linux system: (running directly at the terminal)
Export http_proxy = http: / / proxy IP: port No
export https_proxy='http: / / proxy IP: port No

1 export http_proxy=http://109.123.111.111:8000
2 export https_proxy=http://109.123.111.111:8000

(2) under Windows system: (running directly at the terminal)

Set http_proxy = http: / / proxy IP: port number port '

Set http_proxy = http: / / proxy IP: port number port '

1 set http_proxy=http://109.123.111.111:8000
2 set https_proxy=http://109.123.111.111:8000

[note]: after setting, you may not be able to connect to the Internet when ping is used, but you can when pip is used. Because the ping protocol is different, you cannot use this proxy

3) . permanent settings: (open profile)

(1) open the pip.conf configuration file under Linux system to add the following contents:

  [global]

proxy=http://xxx.xxx.xxx.xxx:8080 ා replace your own proxy address

 1  1 # stay.pip Catalog creation and editing pip.conf(When there is no profile)
 2  2 mkdir ~/.pip     # Establish pip Folder
 3  3 cd ~/.pip          # Establish pip.conf file
 4  4 
 5  5 vi pip.conf        # open pip.conf file
 6  6 
 7  7 # Press i Enter edit mode:
 8  8 [global]
 9  9 proxy=http://10.20.217.2:8080
10 10 
11 11 # Press ESC key,Re input wq Save.

(2) . open pip.ini configuration file under Windows system to add the following contents:

    [global] 

    proxy=http://xxx.xxx.xxx.xxx:8080

1 # stay pip Catalog creation and editing pip.ini(When profile does not exist)
2 cd C:\Users\(Your user name)   
3 mkdir pip                # Establish pip Folder
4 cd pip                     # Get into pip Under path directory
5 cd.>pip.ini              # Establish pip.ini file
6 
7 # Then open C:\Users(User name)\pip\pip.ini,Add the following:
8 [global]
9 proxy=http://10.20.217.2:8080

[supplement]: it can also be set permanently in the / etc/profile configuration file under Linux system

vim /etc/profile:

export http_proxy='http: / / proxy IP: port number '

export https_proxy='http: / / proxy IP: port number '

source /etc/profile

pip add image source

1) . single setting: (same for Windows and Linux systems)

Add the image directly in pip: pip install some package - I image address

1 pip install pandas -i https://mirrors.aliyun.com/pypi/simple/

2) . permanent settings

(1) . open pip.conf configuration file under Linux system to add the following contents:

      [global]

Index url = image address

 1 # stay.pip Catalog creation and editing pip.conf(When there is no profile)
 2 mkdir ~/.pip     # Establish pip Folder
 3 cd ~/.pip          # Establish pip.conf file
 4 
 5 vi pip.conf        # open pip.conf file
 6 
 7 # Press i Enter edit mode:
 8 [global]
 9 index-url = https://pypi.mirrors.ustc.edu.cn/simple/
10 
11 # Press ESC key,Re input wq Save.

(2) . open pip.ini configuration file under Windows system to add the following contents:

      [global]

Index url = image address

1 cd C:\Users\(Your user name)   
2 mkdir pip                # Establish pip Folder
3 cd pip                     # Get into pip Under path directory
4 cd.>pip.ini              # Establish pip.ini file
5 
6 # Then open C:\Users(User name)\pip\pip.ini,Add the following:
7 [global]
8 index-url = https://pypi.mirrors.ustc.edu.cn/simple/

Example of pip adding a mirror and setting up a proxy at once

1 pip install -i https://pypi.douban.com/simple pandas --proxy=http://10.20.217.2:8080

Example of pip permanently adding a mirror and setting up a proxy

1 # Open the corresponding configuration file and add the following( pip Install the required https When encrypting, you need to add trusted-host)
2 [global]
3 index-url = http://mirrors.aliyun.com/pypi/simple/  # Alibaba cloud image source is used here
4 proxy=http://xxx.xxx.xxx.xxx:8080                   # Replace your own proxy address
5 
6 [install]
7 trusted-host=mirrors.aliyun.com                     # Trust alicloud's image source, otherwise there will be a warning

conda setting up proxy and adding image

You only need to set it in the. condarc configuration file (windows and Linux are both files). In general, the. condarc file is found in the C: \ users \ < username > \ directory of the windows system to open and modify. In the Linu system, the terminal can open the file by entering vim ~/.condarc.

If the file does not exist, create a. condarc file command:

1 conda config --add channels r

The configuration content of the. condarc file is as follows (detailed explanation):

 1 # Add mirror source to speed up download
 2 channels:
 3   - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/   # (Add appropriate image source as required)
 4   - defaults
 5 
 6 # Show channel URLs when displaying what is going to be downloaded and in 'conda list'
 7 show_channel_urls: true
 8 allow_other_channels: True
 9 
10 # Set the agent
11 proxy_servers:
12   http: http://xxx.xxx.xxx.xxx:8080
13   https: https://xxx.xxx.xxx.xxx:8080
14 
15 # Set up verify=False remove SSL When authenticating, resolve InsecureRequestWarning warning
16 ssl_verify: false

conda to check whether the source and agent modifications are successful:

1 conda info                       # Check whether the source settings are valid(channel urlsd Changes) 
2 
3 conda config --show              # Command view related information
4 
5 conda config --show-sources      # Where to view the profile


Tags: Python pip Windows Linux

Posted on Fri, 14 Feb 2020 01:15:53 -0500 by jkewlo