centos7 update python2 to python3

After linux centos7, it is necessary to upgrade python2.7 to python3 installation. Because most applications still use pytho3.6. In centos, 2.7 yum should be reserved.
Old python version: 2.7.5

New python version: 3.6.8 Less
talk, start:

Please install openssl before starting to install python3. Otherwise, verification error occurs when pip3 installs virtualenv.yum install openssl openssl-devel

  1. From the Python official website to get the Python3 package, switch to the directory / usr / local / src and
    Insert picture description here
    execute the command line:wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
    Insert picture description here

  2. Use the command, the following command to decompress:
    tar -xzf Python3.6.8.tgz
    Insert picture description here

  3. Create a directory under the / usr / local path – python3
    mkdir /usr/local/python3

  4. Compile and install
    A. Execute command: cd /usr/local/src/Python-3.6.8
    Insert picture description here
    B. Execute command: ./configure --prefix=/usr/local/python3
    Insert picture description here
    C. Execute command:make all

    D. Execute the command: make install

  5. Enter the absolute path of the installation and check whether the installation is successful.
    Run the command:/usr/local/python3/bin/python3.6 -V
    Insert picture description here

  6. Check the environment variables, and search by default in the order of PATH when starting python.
    Excuting an order:echo $PATH
    Insert picture description here

  7. Modify the soft connection, point to python3.6 when starting python
    A. Python
    execute command before backup : mv / usr / bin / python /usr/bin/python2.7.5 (please check your own python version first)
    Insert picture description here
    B. Modify the soft connection
    Execute command: ln -s / usr / local / python3 / bin / python3 / usr / bin / python
    Insert picture description here
    C. View python version:
    execute command:python
    Insert picture description here

  8. In order to input only when it is convenient, you can add alias
    A. execute command: cd ~
    B. execute command: cat .bashrc
    C. execute command:source .bashrc
    Insert picture description here

  9. Check whether python3 is started by default to
    execute the command:python3 -V
    Insert picture description here

  10. Modify the yum file, because after upgrading the version, yum will report an error:
    execute the command: sudo vim /usr/bin/yum
    Insert picture description here
    follow the old version number after the first line!
    Insert picture description here

  11. Next, an error will be reported when installing the software:
    Insert picture description here
    Solution:
    Execute the command: sudo vim /usr/libexec/urlgrabber-ext-down
    In the first line of the file, also change to python2.7 (2.7 is the version, change it according to your own version!)

  12. Related pip3
    ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
    check version
    pip3 -V

Note that pip3 is included when compiling python3. If not, please use make when compiling python3

  1. Use pip3 to install virtualenv.
    Before using pip3 to install virtualenv, first install openssl. Otherwise, it will prompt an error. After
    yum install openssl openssl-devel
    installing openssl and recompiling python3, install virtualenv.
    pip3 install virtualenv

  2. Also, if there is a garbled situation when pressing the backspace key (or arrow keys),
    yum install readline-devel readline
    then recompile.

Published 18 original articles · praised 31 · 50,000+ views

Guess you like

Origin blog.csdn.net/alvinlyb/article/details/103755294