Build python dual version environment under centos

Catalog

Build python dual version environment under centos

centos7 comes with python, version 2.7. But in production environments, higher versions of Python may be required, even if dual versions of Python are needed simultaneously. Next, we manually install Python 3 and configure it to work side by side.

I. Installation of Python 3

1. Clear up the location of python

[root@centos ~]# whereis python
python: /usr/bin/python2.7 /usr/bin/python /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz

So we know that our python is in the / usr/bin directory

[root@centos ~]# cd /usr/bin/
[root@centos bin]# ll python*
lrwxrwxrwx. 1 root root    7 2 Month 709:30 python -> python2
lrwxrwxrwx. 1 root root    9 2 Month 709:30 python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 8 Month 42017 python2.7

As you can see, python points to python 2, python 2 points to python 2.7, so we can install python 3, then point python to python 3, and then python 2 points to python 2.7, so the two versions of python can coexist.

2. Update related packages for downloading and compiling Python 3

yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make

Run this command to update

3. Installation of pip

yum -y install epel-release
yum install python-pip

Run the above two commands to install pip

4. Installing wget with pip

pip install wget

Run the above command to install wget

5. Download the source package of Python 3 with wget

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

By executing the above command, you can download the tar.xz source package for Python 3

6. Compiling Python 3 source packages

# First decompress the tar.xz package
xz -d Python-3.6.4.tar.xz
tar -xf Python-3.6.4.tar

# Then enter the decompressed directory first, and then execute the following commands to compile manually.
cd Python-3.6.4
./configure prefix=/usr/local/python3
make && make install

# If the error can't decompress data; zlib not available occurs, you need to install the relevant library, and if it is normal, you do not need to execute the following command.
#Installation depends on zlib, zlib-devel
yum install zlib zlib
yum install zlib zlib-devel

If no error is prompted at the end, it means that the installation is correct. There will be Python 3 directory in / usr/local / directory.

7. Adding Soft Links

#Back up the original link
mv /usr/bin/python /usr/bin/python.bak
 
#Add Python 3 soft links
ln -s /usr/local/python3/bin/python3.6 /usr/bin/python
 
#Test whether the installation was successful
python -V

8. Change the yum configuration

Change the yum configuration because it requires Python 2 to execute, otherwise Yum will not work properly

vi /usr/bin/yum
//Modify #!/usr/bin/python to #!/usr/bin/python 2
 
vi /usr/libexec/urlgrabber-ext-down
//Modify #!/usr/bin/python to #!/usr/bin/python 2

9. Using python dual version

[root@centos ~]# python2
Python 2.7.5 (default, Jun 20 2019, 20:27:34) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@centos ~]# python
Python 3.6.4 (default, Sep 27 2019, 16:54:08) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

So far, both versions of python have been used properly.

2. Creating a Virtual Environment

Usually in the production environment, we do not use the python environment of the system directly, because it is difficult to manage all kinds of dependency packages, so it will be much better to use the virtual environment. For each specific project, we can use a separate virtual environment, so that the environments can be used independently and the dependencies do not affect each other.

1. Use virtualenv

install

pip install virtualenv

Create a virtual environment for a project

$ cd my_project_dir
$ virtualenv venv  # venv is the directory name of virtual environment, and the directory name is customized

Viralenv venv will create a folder in the current directory containing Python executable files and a copy of the pip library so that other packages can be installed. The name of the virtual environment (venv in this case) can be arbitrary; omitting the name will place the files in the current directory. In any directory where you run commands, this creates a copy of Python and places it in a file called venv.

You can choose to use a Python interpreter:

$ virtualenv -p /usr/bin/python2.7 venv    # - p parameter specifies Python interpreter program path

This will use the Python interpreter in / usr / bin / Python 2.7.

Start using virtual environments

$ source venv/bin/activate # activation

From now on, any packages you install using pip will be placed in the venv folder, isolated from the global installation of Python.

Install packages as usual, such as:

pip install requests

Quit using virtual environment

If you temporarily complete your work in a virtual environment, you can disable it:

$ . venv/bin/deactivate

This will go back to the system's default Python interpreter, including the installed libraries.

To delete a virtual environment, just delete its folders. (Execute Rm-rf venv).

There is some inconvenience in virtualenv, because the start and stop scripts of virtual are in specific folders. You may have many virtual environments scattered around the system after a period of time. You may forget their names or locations.

2. Use virtualenvwrapper

Because virtualenv is not convenient for centralized management of virtual environment, it is recommended to use virtualenvwrapper directly. Virtualenvwrapper provides a series of commands to facilitate working with virtual environments. It puts all your virtual environments in one place.

install

linux installs virtualenvwrapper (make sure virtualenv is installed)

pip install virtualenvwrapper

To configure

After installation, write the following in ~/.bashrc

export WORKON_HOME=~/Envs
source /usr/local/bin/virtualenvwrapper.sh 
  • The first line: virtual environment vwrapper holds the directory of the virtual environment
  • Line 2: virtrualenvwrapper is installed in python's bin directory, so the path is bin/virtualenvwrapper.sh in python's installation directory.
source ~/.bashrc # Read in the configuration file and take effect immediately

Possible problems

  • Question 1
/bin/python: No module named virtualenvwrapper
virtualenvwrapper.sh: There was a problem running the initialization hooks.

If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/bin/python and that PATH is
set properly.
  • Question 2
[root@centos ~]# source .bashrc
-bash: /usr/local/bin/virtualenvwrapper.sh: No such file or directory
  • Solution
    • Question 1

    Write the following in ~/.bashrc

    export VIRTUALENVWRAPPER_PYTHON=/usr/local/python36/bin/python3 # Specify the python interpreter path for virtual use
    • Question 2

    Viralenvwrapper. sh can't find the error, find it and copy it to / usr/local/bin / below

    Then execute source ~/.bashrc

Start using

  • Creating a Virtual Environment
mkvirtualenv venv # venv is the name of the virtual environment, custom

This creates a new virtual environment named venv in the directory specified by the WORKON_HOME variable. If you want to specify the python version, you can specify the Python interpreter by "--python"

mkvirtualenv --python=/usr/local/python3.5.3/bin/python venv
  • View the current virtual environment directory
[root@centos ~]# workon
  • Switching to Virtual Environment
[root@centos ~]# workon py3
(py3) [root@centos ~]#
  • Exit from Virtual Environment
(py3) [root@centos ~]# deactivate
[root@centos ~]# 
  • Delete virtual environment
rmvirtualenv venv

Keywords: Python CentOS pip yum

Added by deffe on Mon, 14 Oct 2019 10:40:07 +0300