Maybe you will encounter such a problem in your daily work. Now the basic Linux system comes with the old version of python2.7. X, and I don’t want to use the old version, but there may be problems in upgrading directly, or the programs that depend on the old version can’t run. Is there any way to install the new version of 3. X?

The answer is yes. Please use pyenv to manage multiple versions of Python. You can use it at will.

In fact, the official support will no longer be provided by the end of 2019. As of the countdown, there are six days left: https://pythonclock.org/

Then someone may ask, under a system, different libraries depend on different versions of python, for example, python 2. X requires Django 1.0, python 3. X requires Django 2.0, so the system can only install one Django version, so how can we solve this problem?

The answer is yes. Pyenv-virtualenv, the plug-in of pyenv, creates two virtual environments, independent of each other and does not affect each other.

Experimental environment:

Linux system: CentOS 7 X64

Pyenv common commands:

[Python @ localhost ~] $pyenv install - L // view the available installation versions
[Python @ localhost ~] $pyenv install 3.6.9 // install Python 3.6.9 Online
[Python @ localhost ~] $pyenv virtualenv 3.6.9 PY3 // create a virtual environment. 3.6.9 is the python version and PY3 is the alias
[Python @ localhost test] $pyenv local 3.6.9 // enter the directory, set or display the local Python Version (this directory is valid)
[Python @ localhost test] $pyenv global system // set or display the global Python version
[Python @ localhost test] $Python version // displays the current Python version
[Python @ localhost test] $pyenv versions // displays all available Python versions
[Python @ localhost test] $pyenv update // update pyenv
[Python @ localhost ~] $pyenv virtualenvs // view all virtual environments
[Python @ localhost ~] $RM - fr ~ /. Pyenv // uninstall pyenv
[Python @ localhost ~] $pyenv virtualenv delete PY3 // delete virtual environment

Create user name and password:
Note: don’t use root account, form habit

[[email protected] ~]$ useradd python
[[email protected] ~]$ su - python
[[email protected] ~]$ echo python | passwd python --stdin

Install dependent components:

[[email protected] ~]$ yum install gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel
zlib-devel bzip2-devel git curl

To install pyenv:

#Method 1: online installation
[[email protected] ~]$ curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash

#Method 2: to avoid being unable to install online due to wall restrictions, copy the following linked shell script to the local pyenv.sh:
https://github.com/pyenv/pyenv-installer/blob/master/bin/pyenv-installer

[Python @ localhost ~] $touch pyenv.sh // create a new file, paste the shell code above, and save it
[[email protected] ~]$ bash pyenv.sh

After installation, follow the prompts to set the system environment variable, and add the following code in ‘. Bashrc’:

[[email protected] ~]$ vim .bashrc 

#At the bottom, add the following script
export PATH="/home/python/.pyenv/bin:$PATH"  
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Restart the shell for the path change to take effect:

[[email protected] ~]$ exec $SHELL 

Download the python installation package in advance and upload it to CentOS directory through winscp:

  • Python-3.6.9.tar.xz
  • Python-3.8.0.tar.xz
#Create the cache directory in the. Pyenv directory, which has been uploaded through winscp
[[email protected] ~]$ cd .pyenv
[[email protected] .pyenv]$ mkdir cache
[[email protected] cache]$ ll
Total consumption 34224
-Rw-rw-r --. 1 Python 17212164 December 15 01:56 python-3.6.9.tar.xz
-Rw-rw-r --. 1 Python 17829824 December 15 01:57 python-3.8.0.tar.xz

Note: if you install directly through ‘pyenv install 3.6.9’, it will be slow because you need to connect to foreign countries.

Start installation

[Python @ localhost PKG] $pyenv install 3.6.9 - v v v // - V can see the detailed installation process
[Python @ localhost ~] $pyenv install 3.8.0 - v v v // - V can see the detailed installation process

To create a project folder:

#Here's a chestnut:
 #1. Python directory, using Python version 3.6.9
 #2. My ABCD directory, using Python version 3.6.9
 #3. Test directory, using Python version 3.8.0

[[email protected] Python]$ mkdir -pv network/Python/
[[email protected] Python]$ mkdir -pv network/My_django/
[[email protected] Python]$ mkdir -pv network/test/

Create a virtual environment with the following effects:

#Python Directory:
[Python @ localhost ~] $pyenv virtualenv 3.6.9 PY3 // set alias PY3
[[email protected] Python]$ pyenv local py3
(py3) [[email protected] Python]$ pyenv version
py3 (set by /home/python/network/Python/.python-version)
(PY3) [Python @ localhost Python] $// see (PY3) on the left to indicate that it is in a virtual environment and independent

#My Django Directory:
[Python @ localhost ~] $pyenv virtualenv 3.6.9 py369 // set alias py369
[[email protected] My_django]$ pyenv local py369
(py369) [[email protected] My_django]$ pyenv version
py369 (set by /home/python/network/My_django/.python-version)
(py369) [[email protected] My_django]$

#Test directory:
[Python @ localhost ~] $pyenv virtualenv 3.8.0 py380 // set alias py380
[[email protected] test]$ pyenv local py380
(py380) [[email protected] test]$ pyenv version
py380 (set by /home/python/network/test/.python-version)
(py380) [[email protected] test]$

#Verification effect
  #Install PIP install redis in my Django virtual environment
  #No redis in other virtual directories
#My Django virtual environment
(py369) [[email protected] My_django]$ pip list
Package    Version
---------- -------
pip        19.3.1 
redis      3.3.11 
setuptools 40.6.2
#Python virtual environment
[[email protected] Python]$ pip list
Package    Version
---------- -------
pip        19.2.3 
setuptools 41.2.0

Note: you can install the required software version in each project, which is independent and does not affect each other.

Pyenv official link


If you like my article, please pay attention to my public number: drip technology, scan code attention, and share it regularly.

Pyenv virtual environment management Python multi version and software library