Maybe you will encounter this problem in your daily work, now the basic linux system comes with an old version of python2.version, and I don't want to use the old version, but the direct upgrade may be a problem, or rely on the old version of the program can't run, is there any way to install the new version?
The answer is yes, please use pyenv, which supports managing multiple python versions at will.
In fact, official support will no longer be available at the end of 2019, with 6 days to go as of the countdown:/
Then someone may ask, that in a system, different libraries rely on different python version, for example, version needs django1.0 version, version needs django2.0 version, then so that the system can only install a django version, which how to solve it?
The answer is yes, pyenv comes with a plugin, pyenv-virtualenv, that creates two virtual environments that are independent of each other and do not affect each other.
Experimental environment:
linux system: CentOS 7 x64
Common commands for pyenv:
[python@localhost ~]$ pyenv install -l //View available installed versions [python@localhost ~]$ pyenv install 3.6.9 // Install python version 3.6.9 online [python@localhost ~]$ pyenv virtualenv 3.6.9 py3 //create virtual environment, 3.6.9 for python version, py3 for alias [python@localhost test]$ pyenv local 3.6.9 // Go to the directory and set or display the local python version (valid for this directory) [python@localhost test]$ pyenv global system // Set or display the global python version. [python@localhost test]$ pyenv version // Display the current python version [python@localhost test]$ pyenv versions // Show 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 username and password.
Note: Don't use a root account, make it a habit
[root@localhost ~]$ useradd python [root@localhost ~]$ su - python [python@localhost ~]$ echo python | passwd python --stdin
Install dependent components:
[python@localhost ~]$ yum install gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel git curl
Install pyenv:
# Method 1: Online Installation [python@localhost ~]$ curl -L /pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash #Method 2: In order to avoid the wall limitation and unable to install online, copy the shell script from the following link to local: /pyenv/pyenv-installer/blob/master/bin/pyenv-installer [python@localhost ~]$ touch // create a new file, paste in the shell code above, save it [python@localhost ~]$ bash
After the installation is complete, follow the prompts to set the system environment variables and add the following code to '.bashrc':
[python@localhost ~]$ 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 to make the path changes take effect:
[python@localhost ~]$ exec $SHELL
Download the python installer from the official website in advance and upload it to the centos directory via winSCP:
- Python-3.6.
- Python-3.8.
# Create the cache directory in the .pyenv directory, already uploaded via winSCP [python@localhost ~]$ cd .pyenv [python@localhost .pyenv]$ mkdir cache [python@localhost cache]$ ll total usage 34224 -rw-rw-r--. 1 python python 17212164 12moon 15 01:56 Python-3.6. -rw-rw-r--. 1 python python 17829824 12moon 15 01:57 Python-3.8.
Note: If you go directly to install via 'pyenv install 3.6.9 ', it will be slow because you have to connect to a foreign country.
Start Installation
[python@localhost pkg]$ pyenv install 3.6.9 -vvv //-v to see the installation in detail [python@localhost ~]$ pyenv install 3.8.0 -vvv //-v to see the installation in detail
Create a project folder:
#A chestnut. # catalog, using python version 3.6.9 #2.My_django directory, using python version 3.6.9 # catalog, using python version 3.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 with the following results:
# Python directory: [python@localhost ~]$ pyenv virtualenv 3.6.9 py3 // Set the alias 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]$ // Seeing (py3) on the left means it's in the virtual environment and is standalone #My_django directory: [python@localhost ~]$ pyenv virtualenv 3.6.9 py369 // Setting the alias 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 directory: [python@localhost ~]$ pyenv virtualenv 3.8.0 py380 // Setting the alias 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]$ #Verify the effect #install pip install redis in My_django virtual environment # Other virtual directories do not have redis #My_django virtual environment (py369) [python@localhost My_django]$ pip list Package Version ---------- ------- pip 19.3.1 redis 3.3.11 setuptools 40.6.2 #Python virtual environment [python@localhost Python]$ pip list Package Version ---------- ------- pip 19.2.3 setuptools 41.2.0
Note: You can install the required software versions in each project, all independent of each other.
pyenv official link
This is the whole content of this article.