centos python build a double version environment

centos python build a double version environment

centos7 comes with python, version python2.7. But in a production environment, often it may require a higher python version, or even a situation requires a dual version of the python used simultaneously. Next, we manually install python3, and configuration can coexist after use.

First, install python3

1, to clarify the position comes 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

We thus learn python in / usr / bin directory

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

You can see, python points to python2, python2 points to python2.7, so we can put on a python3, and then point to the python python3, then python2 point python2.7, then the two versions of python will be able to coexist.

2, the relevant update package for downloading compilation of python3

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

Run this command to update

3, install pip

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

Run command to install more than two pip

4, with mounted pip wget

pip install wget

Run the above command to install wget

5, with the source package wget download python3

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

Execute these commands, you can download the source package python3 of tar.xz

6, the compiler source package python3

# 先对tar.xz包解压
xz -d Python-3.6.4.tar.xz
tar -xf Python-3.6.4.tar

# 然后先进入到解压后的目录中,依次执行下面命令进行手动编译
cd Python-3.6.4
./configure prefix=/usr/local/python3
make && make install

# 如果出现can't decompress data; zlib not available这个错误,则需要安装相关库,如果正常,则不必执行下面的命令。
#安装依赖zlib、zlib-devel
yum install zlib zlib
yum install zlib zlib-devel

If the last did not prompt an error, on behalf of correctly installed in / usr / local / directory will be a directory python3

7, add a soft link

#将原来的链接备份
mv /usr/bin/python /usr/bin/python.bak
 
#添加python3的软链接
ln -s /usr/local/python3/bin/python3.6 /usr/bin/python
 
#测试是否安装成功了
python -V

8, yum configuration change

Yum configuration change, because it use to python2 to perform, otherwise it will lead yum not working

vi /usr/bin/yum
把#! /usr/bin/python修改为#! /usr/bin/python2
 
vi /usr/libexec/urlgrabber-ext-down
把#! /usr/bin/python 修改为#! /usr/bin/python2

9, using python double 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, python can double version has been properly used.

Second, create a virtual environment

Generally in a production environment, we do not directly use the system python environment, such as the presence of various dependencies difficult to manage, and therefore, would be much better to use a virtual environment, each specific project, we can use a single virtual environments, each independently between this environment, dependent of each other.

1, virtualenv

installation

pip install virtualenv

Create a virtual environment for a project

$ cd my_project_dir
$ virtualenv venv  # venv为虚拟环境目录名,目录名自定义

virtualenv venv will be created in the current directory in a folder that contains Python executable file, and a copy pip library, so you can install additional package. Virtual environment name (in this case is venv) is arbitrary; if the file name will be omitted are in the current directory. In any directory you run the command, which creates a copy of Python, and place it on the file called venv.

You can choose to use a Python interpreter:

$ virtualenv -p /usr/bin/python2.7 venv    # -p参数指定Python解释器程序路径

This will use the /usr/bin/python2.7 Python interpreter.

Start using a virtual environment

$ source venv/bin/activate # 激活

From now on, whatever you use pip install the package will be placed venv folder, and cut off from the global installation of Python.

Usual installation package, such as:

pip install requests

Quit using a virtual environment

If you temporarily completed the work in a virtual environment, you can disable it:

$ . venv/bin/deactivate

It will return to the default Python interpreter, including installed library will return to the default.

To delete a virtual environment, simply delete its folder. (Execute rm -rf venv).

Here virtualenv some inconvenience, because virtual start, stop scripts in a specific folder, probably after some time, you may have a lot of virtual environments scattered throughout the system, you might forget their names or location.

2, virtualenvwrapper

Given virtualenv not easy centralized management of virtual environments, it is recommended to use direct virtualenvwrapper. virtualenvwrapper provides a set of command and make virtual work environment is facilitated. It put all your virtual environment in one place.

installation

linux install virtualenvwrapper (virtualenv make sure you have installed)

pip install virtualenvwrapper

Configuration

After installation is complete, the following is written in ~ / .bashrc

export WORKON_HOME=~/Envs
source /usr/local/bin/virtualenvwrapper.sh 
  • The first line: virtualenvwrapper virtual environment storage directory
  • The second line: virtrualenvwrapper installed to python bin directory, so the path is python installation directory bin / virtualenvwrapper.sh
source ~/.bashrc # 读入配置文件,立即生效

potential problem

  • 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

    In ~ / .bashrc writes the following

    export VIRTUALENVWRAPPER_PYTHON=/usr/local/python36/bin/python3 # 指定虚拟使用的python解释器路径
    • Question 2

    virtualenvwrapper.sh not find the error, after finding copied to / usr / local / bin / under

    Then performs source ~ / .bashrc 

start using

  • Create a virtual environment
mkvirtualenv venv # venv为虚拟环境的名字,自定义

This will create a new virtual environment called venv in WORKON_HOME variable specified directory. To specify python version, python interpreter can be specified by "--python"

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

Guess you like

Origin www.cnblogs.com/luyuze95/p/11671787.html