Install Python 3.0 in CcentOS7 8 and create virtual environment

Posted by mattee on Thu, 27 Jan 2022 14:18:41 +0100

Install Python 3.0 in CcentOS7 8 and virtual environment

Install Python 3 eight

Install dependent packages

Download Python version

Install Python installation package

Create soft connection

yum configuration

virtual environment

Update pip

Install virtualenv, virtualenvwrapper

Define virtualenvwrapper path

Create virtual environment

Other virtual environment commands

Install Python 3 eight

First, open the terminal, enter su and enter the root permission, as shown in the following figure:

Install dependent packages

After entering the root permission, enter the following codes in sequence to install the dependent package:

yum -y groupinstall "Development tools"
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

Download Python version

After installing the dependency package, download the python version you need. For example, the version downloaded here is Python 3 eight point six

yum -y install wget
wget https://www.python.org/ftp/python/3.8.6/Python-3.8.6.tar.xz

Install Python installation package

Before extracting the python installation package, we first create a new folder to store Python 3. The code is as follows:

mkdir /usr/local/python3 

Move the Python installation package to the newly created folder. The code is as follows:

mv Python-3.8.6.tar.xz /usr/local/python3

Enter the python 3 directory, and the code is as follows:

cd /usr/local/python3

Enter the following code in sequence to extract and install the Python installation package:

tar -xvJf  Python-3.8.6.tar.xz
cd Python-3.8.6
./configure --prefix=/usr/local/python3
make
make install

Create soft connection

In order to better use the python 3 version, we need to create a soft connection. The soft connection is equivalent to a shortcut to windows. Its syntax structure is:

ln -s  Source directory destination soft connection

Here, we create a soft connection for the python 3 version, and execute the following code in sequence:

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

View current soft connection:

ll /usr/bin/ |grep python3

yum configuration

Because it needs python2 to execute, otherwise Yum will not work normally. In order to make Python 2 and python 3 coexist, we need to modify the yum configuration:

vi /usr/bin/yum 
Put the first line#! /usr/bin/python Change to #! /usr/bin/python2 
vi /usr/libexec/urlgrabber-ext-down 
Put the first line#! /usr/bin/python Change to #! /usr/bin/python2
vi /usr/bin/yum-config-manager
 Put the first line#!/usr/bin/python -tt Change to #!/usr/bin/python2 -tt

At this time, python 3 8 is installed. Enter python3, as shown in the following figure:

virtual environment

Update pip

Before using the pip installation package, you need to update the pip. The code is as follows:

pip3 install --upgrade pip

Install virtualenv, virtualenvwrapper

pip3 install virtualenv
pip3 install virtualenvwrapper

Define virtualenvwrapper path

Before defining the path of virtualenvwrapper, you need to create the directory where the virtual environment is stored and find the path of virtualenvwrapper

mkdir $HOME/.virtualenvs
find / -name virtualenvwrapper.sh 

As shown in the figure below:

get into. In the bashrc file, define the virtualenvwrapper path. The code is as follows:

vim ~/.bashrc

get into. After the bashrc file, write the following code at the end of the text and save it:

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/python3/bin/virtualenv
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/python3/bin/virtualenvwrapper.sh

As shown in the figure below:

function. bashrc file, the code is as follows:

source ~/.bashrc

Create virtual environment

First, create the soft connection of virtualenv. The code is as follows:

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

To create a virtual environment, the code is as follows:

mkvirtualenv -p /usr/bin/python3 py38

Here, we create a virtual environment with Python version of Python 3 and virtual environment name of py38, as shown in the following figure:

Other virtual environment commands

workon py38		#Enter the virtual environment named py38
deactivate		#Exit virtual environment
lsvirtualenv -b	 #List all virtual environments
rmvirtualenv py38  #Delete the virtual environment named py38

OK, CentOS7 installs Python 3 That's all for 8 and creating a virtual environment. Thank you for watching!!!

Topics: Python Linux CentOS