SoFunction
Updated on 2024-10-28

The python method for setting up a proxy and adding a mirror source

Why do I need to modify the mirror source?

General use of python install libraries, will use pip install xxx command or conda install xxx command, because pip and conda default foreign mirror source, this will be in the Python official source/pypi download, the speed is very slow, sometimes because of the timeout will be thrown abnormally can not be downloaded successfully, so you can choose some more stable speed So you can choose some more stable and faster domestic mirrors to download python libraries, which can significantly speed up module installation.

Why do I need to set up a proxy?

When the computer is restricted access to the Internet (company security considerations) can not be connected to the outside world, at this time you need to set up a proxy to solve the problem; usually the company requires networking through a proxy to ensure network security (i.e., the network needs to be set up to access the outside world proxy); sometimes due to the official foreign pypi is walled, resulting in unavailability, but also through the setting of the proxy to access.

Note: pip and conda modify the image source differently.

configuration file

Before setting up a proxy and adding a mirror source, check if the configuration file exists, and create it if it doesn't. The path to the configuration file (the path varies from system to system):

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

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

3). Windows: %APPDATA%\pip\, %APPDATA% means the actual path, such as C:\Users\user_xxx\AppData\Roaming, you can execute echo %APPDATA% command in cmd to check it.

[Addendum]:

1). Any pip command line argument can have its default value defined in the configuration file!

2). which pip can point to the location of pip (e.g. /usr/local/bin/pip)

pip setup proxy

1). Single setup: (same for Windows and Linux systems)

Just set the proxy directly at pip:pip install some-package --proxy=http:// proxy server IP:port number

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

2). Temporary settings (expires after reconnection):

(1). Under Linux system: (run directly in the terminal)

  export http_proxy='http://proxy serverIP:port numberport'
  export https_proxy='http://proxy serverIP:port numberport'

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

(2). Windows: (run it directly from the terminal)

  set http_proxy='http://proxy serverIP:port numberport'

  set http_proxy='http://proxy serverIP:port numberport'

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

Note: You may not be able to connect to an external network when using ping after setting up, but you can use this proxy when using pip, because the protocol of ping is not the same,

3). Permanent settings: ( open configuration file )

(1). Open the configuration file under Linux system and add the content as follows:

[global]

proxy=:8080 # Substitute out your own proxy address

  # Create and edit in the .pip directory (when there is no configuration file)
  mkdir ~/.pip   # Create pip folder
  cd ~/.pip     # Create documents
  vi     # Open the file
  # Press i to enter edit mode:
  [global]
  proxy=http://10.20.217.2:8080
 # check or refer toESClinchpin,retypewqJust save it.。

(2). Open the configuration file under Windows system and add the contents as follows:

    [global]

    proxy=:8080

 # Create and edit in pip directory (when configuration file does not exist)
 cd C:\Users\(Your username)  
 mkdir pip        # Create pip folder
 cd pip           # Go to the pip path directory
 cd.>       # Create documents
 # Then open C:\Users(user name)\pip\ and add the following:
 [global]
 proxy=http://10.20.217.2:8080

[Addition]: Linux systems can also be set permanently in the /etc/profile configuration file.

vim /etc/profile:
  export http_proxy='http://proxy server IP:port number'
  export https_proxy='http://proxy server IP:port number'
source /etc/profile

pip add mirror source

1). Single setup: (same for Windows and Linux systems)

Just add the mirror directly at pip: pip install some-package -i mirror address

 pip install pandas -i /pypi/simple/

2). Permanent settings

(1). Open the configuration file under Linux system and add the content as follows:

      [global]

index-url = mirror address

 # Create and edit in the .pip directory (when there is no configuration file)
 mkdir ~/.pip   # Create pip folder
 cd ~/.pip     # Create documents
 vi     # Open the file
 # Press i to enter edit mode:
 [global]
 index-url = /simple/
 # check or refer toESClinchpin,retypewqJust save it.。

(2). Open the configuration file under Windows system and add the following contents:

      [global]

index-url = mirror address

 cd C:\Users\(Your username)  
 mkdir pip        # Create pip folder
 cd pip           # Go to the pip path directory
 cd.>       # Create documents
 # Then open C:\Users(user name)\pip\ and add the following:
 [global]
 index-url = /simple/

Example of pip adding a mirror and setting up a proxy in one go

 pip install -i /simple pandas --proxy=http://10.20.217.2:8080

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

# Open the corresponding configuration file to add the following content, (pip installation needs to use https encryption, you need to add trusted-host)
 [global]
 index-url = /pypi/simple/ # Here we use the mirror source of AliCloud #
 proxy=:8080          # Substitute your own proxy address
 [install]
 trusted-host=           # Trusting AliCloud's Mirror Source,Otherwise there will be a warning

conda setup proxy and add mirrors

Just need to set in the .condarc configuration file (Windows and Linux are this file), in general the .condarc file in the windows system C:\Users\<username>\ directory, find this file to open the modification; in the Linu system, the terminal input vim ~/. condarc to open this file.

If the file does not exist, create one, create .condarc file command.

 conda config --add channels r

The .condarc file is configured as follows (in detail).

 # Add mirror source to speed up download
 channels:
  - /anaconda/pkgs/free/  # (add appropriate mirror sources as required)
  - defaults
 # Show channel URLs when displaying what is going to be downloaded and in ‘conda list‘
 show_channel_urls: true
 allow_other_channels: True 
 # Set the agent
 proxy_servers:
  http: :8080
  https: :8080
 # Resolve InsecureRequestWarning warnings when removing SSL authentication by setting verify=False
 ssl_verify: false

The conda command to see if source and agent modifications were successful.

 conda info            # Check to see if the source settings are valid (channel urlsd changes)
conda config --show       # Command to view related information
 conda config --show-sources   # View the location of the configuration file

summarize

The above is a small introduction to python set proxy and add mirror source introduction, I hope to help you, and thank you very much for your support of my site!