Centos7 installs python3.7 and solves the problem of python version coexistence

1. Installation environment

cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

2. Install epel network yum source

yum -y install epel-release

3. Update yum source

yum clean all

yum makecache

4. Update the rpm package

yum -y update

5. Installation dependent environment

yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel zlib1g-dev zlib*

zlib1g-dev may report that it does not exist, ignore it temporarily

6. Download python3.7

cd /usr/local/src

wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz

If the domestic download is too slow or the link is invalid, please use the following link

wget -c http://itityunwei.cn/linux_package/Python-3.7.0.tar.xz

7. Unzip python3.7

tar -vxf Python-3.7.0.tar.xz (Note: Do not add j parameter)

8. Install python3.7

mkdir -p /usr/local/python3

cd /usr/local/src/Python-3.7.0

./configure --prefix=/usr/local/python3 --enable-optimizations --with-ssl

make && make install

9. Create soft connection (shortcut)

ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3

ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3

ln -s /usr/local/python3/bin/virtualenv /usr/local/bin/virtualenv (does not exist, it will be used to create a virtual environment later, it will be installed later)

10. Check if python3 is successfully installed

python3 -v

pip3 -v

11. Modify pip source to domestic source

cd /root

mkdir .pip

cd .pip

the pip.conf

[global]

index-url = https://mirrors.aliyun.com/pypi/simple

12. Recompile and install python3, add with-ssl

cd /usr/local/src/Python-3.7.0

./configure --with-ssl

make && make install

13. Upgrade pip3 and install virtual environment

pip3 install --upgrade pip

pip3 install virtualenvwrapper

14. Edit the .bash_profile file

cd /root

cp .bash_profile .bash_profile.back

vim .bash_profile

Add the following lines at the bottom

export WORKON_HOME=/Envs
export VIRTUALENVWRAPPER_VIRTUALENV=/Users/mosson/Library/Python/3.7/bin/virtualenv
export VIRTUALENVWRAPPER_PYTHON=/usr/local/python3/bin/python3
source /usr/local/python3/bin/virtualenvwrapper.sh

Effective configuration file

source .bash_profile

15. Create a virtual environment

Create a virtual environment
[root@localhost ~]# mkvirtualenv The name of the virtual environment
If you need to specify other versions of python
[root@localhost ~]# mkvirtualenv -p The directory where the python interpreter is located (The default is /Envs )
Switch virtual environment
[root@localhost ~]# workon virtual environment name
Exit virtual environment
[root@localhost ~]# deactivate
view virtual environment
[root@localhost ~]# lsvirtualenv

16. Solve the problem of coexistence of python2 and python3, in order to quickly switch the python version and support the coexistence of multiple python versions (this method is also applicable to windows, which is my experience for many years)

cd /usr/local/bin/

mv python python2 (Note: python here refers to the python version currently running on the system, the default is python2, which is also the python version read by the system on any global interface. I want python3 to take effect globally instead of python2 as the default version, so To rename python to python2, it is best to rename the original version of the execution program, which is the execution program of python2 by default, rename it to python2)

mv python3 python (Note: To make python3 take effect globally, you need to rename python3 to python so that the system can call python3 to execute. Simply put, just rename the version you want to switch. Of course, the relevant version of the startup program needs Soft link to this directory)

cd

python

Found that python3 has been used as the default startup version

[root@localhost ~]# python
Python 3.7.0 (default, Jan 4 2021, 17:53:08)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type “help”, “copyright”, “credits” or “license” for more information.

exit()

done !

Guess you like

Origin blog.csdn.net/weixin_43838503/article/details/112204715