pyenv python virtual environment management and multi-version software library

We may encounter in their daily work such a problem, and now comes with basic linux systems are older versions of python2.7.x version, I do not want to use the old version, but a direct upgrade may be a problem, or rely the old version of the program will not run, there is no way to install a new version 3.x?

The answer is yes, please use pyenv, can support management of multiple versions of python, any use.

In fact, the official at the end of 2019 will no longer provide support, as well as of the countdown six days: https: //pythonclock.org/

That one might ask, that in a system, different libraries rely on different versions of python, for example python2.x on the version django1.0 version, python3.x version requires django2.0 version, so that the system can only be installed a django version, which in turn how to solve it?

The answer is yes, pyenv comes with plug-pyenv-virtualenv, create two virtual environment, independent of each other, each are not affected.

lab environment:

linux系统:CentOS 7 x64

pyenv commonly used commands:

[python@localhost ~]$ pyenv install -l  //查看可用的安装版本
[python@localhost ~]$ pyenv install 3.6.9  //在线安装python3.6.9版本
[python@localhost ~]$ pyenv virtualenv 3.6.9 py3  //创建虚拟环境,3.6.9为python版本,py3为别名
[python@localhost test]$ pyenv local 3.6.9  //进入目录,设置或显示本地python版本(本目录有效)
[python@localhost test]$ pyenv global system  //设置或显示全局python版本
[python@localhost test]$ pyenv version  //显示当前python版本
[python@localhost test]$ pyenv versions  //显示可用的所有python版本
[python@localhost test]$ pyenv update  //更新pyenv
[python@localhost ~]$ pyenv virtualenvs  //查看所有虚拟环境
[python@localhost ~]$ rm -fr ~/.pyenv  //卸载pyenv
[python@localhost ~]$ pyenv virtualenv-delete py3  //删除虚拟环境

Create a user name and password:
Description: Do not use the root account, habit

[root@localhost ~]$ useradd python
[root@localhost ~]$ su - python
[python@localhost ~]$ echo python | passwd python --stdin

Installation dependent components:

[python@localhost ~]$ yum install gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel
zlib-devel bzip2-devel git curl

Installation pyenv:

#方法1:在线安装
[python@localhost ~]$ curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash

#方法2:为避免受制墙限制而无法在线安装,复制如下链接的shell脚本到本地pyenv.sh:
https://github.com/pyenv/pyenv-installer/blob/master/bin/pyenv-installer

[python@localhost ~]$ touch pyenv.sh  //新建文件,把上面的shell代码贴进来,保存
[python@localhost ~]$ bash pyenv.sh

After installation, follow the prompts, set system environment variables, in '.bashrc' add the following code:

[python@localhost ~]$ vim .bashrc 

#最下面,新增如下脚本
export PATH="/home/python/.pyenv/bin:$PATH"  
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Restart the shell, the route changes to take effect:

[python@localhost ~]$ exec $SHELL 

Ahead of the official download the installation package python and uploaded to centos directory by winSCP:

  • Python 3.6.9.tar.xz
  • Python 3.8.0.tar.xz
#在.pyenv目录下创建cache目录,已通过winSCP上传好
[python@localhost ~]$ cd .pyenv
[python@localhost .pyenv]$ mkdir cache
[python@localhost cache]$ ll
总用量 34224
-rw-rw-r--. 1 python python 17212164 12月 15 01:56 Python-3.6.9.tar.xz
-rw-rw-r--. 1 python python 17829824 12月 15 01:57 Python-3.8.0.tar.xz

NOTE: If you directly through the 'pyenv install 3.6.9' to install, will be very slow, because you want to connect to foreign countries.

start installation

[python@localhost pkg]$ pyenv install 3.6.9 -vvv  //-v 可以看到安装详细的过程
[python@localhost ~]$ pyenv install 3.8.0 -vvv  //-v 可以看到安装详细的过程

Create a project folder:

#举个栗子:
 #1.Python目录,使用python3.6.9版本
 #2.My_django目录,使用python3.6.9版本
 #3.test目录,使用python3.8.0版本

[python@localhost Python]$ mkdir -pv network/Python/
[python@localhost Python]$ mkdir -pv network/My_django/
[python@localhost Python]$ mkdir -pv network/test/

Create a virtual environment, the effect is as follows:

#Python目录:
[python@localhost ~]$ pyenv virtualenv 3.6.9 py3  //设置别名py3
[python@localhost Python]$ pyenv local py3
(py3) [python@localhost Python]$ pyenv version
py3 (set by /home/python/network/Python/.python-version)
(py3) [python@localhost Python]$    //左边看到(py3)说明已在虚拟环境中,是独立的

#My_django目录:
[python@localhost ~]$ pyenv virtualenv 3.6.9 py369  //设置别名py369
[python@localhost My_django]$ pyenv local py369
(py369) [python@localhost My_django]$ pyenv version
py369 (set by /home/python/network/My_django/.python-version)
(py369) [python@localhost My_django]$

#test目录:
[python@localhost ~]$ pyenv virtualenv 3.8.0 py380  //设置别名py380
[python@localhost test]$ pyenv local py380
(py380) [python@localhost test]$ pyenv version
py380 (set by /home/python/network/test/.python-version)
(py380) [python@localhost test]$

#验证效果
  #在My_django虚拟环境下安装pip install redis
  #其他虚拟目录没有redis
#My_django虚拟环境
(py369) [python@localhost My_django]$ pip list
Package    Version
---------- -------
pip        19.3.1 
redis      3.3.11 
setuptools 40.6.2
#Python虚拟环境
[python@localhost Python]$ pip list
Package    Version
---------- -------
pip        19.2.3 
setuptools 41.2.0 

Note: You can install the software versions required for each project, are independent of each other.

pyenv official links


If you like my articles, I welcome the attention of the public number: drip technique, scan code concerned, from time to time to share

Bit technology

Guess you like

Origin www.cnblogs.com/singvis/p/12099797.html