python management tool pyenv

python management tool pyenv

[Abstract]
As a python back-end development engineer, we need to switch the python version frequently to work, which means that our system needs to install multiple python versions. Generally, the unix system comes with Python2.7.5 by default. If we need to install python3, The first way can be to download the source package and then compile and install (make && make install), but the simpler way should be to use the pyenv tool to install, combined with the pyenv-virtualenv plug-in, you can easily create and manage python virtual environment, in Various operations in the virtual environment will not affect the normal operation of the system, and effectively prevent system crashes caused by random installation of python versions. This article mainly explains the installation and use of pyenv and pyenv-virtualenv.

[Text]
1. Installation of pyenv tool and pyenv-virtualenv plug-in

Installation environment: centos7.4
pyenv: python version management tool
pyenv-virtualenv: pyenv plugin for creating python virtual environments

1.1. Install dependent packages
yum -y install gcc gcc-c++ make git patch openssl-devel zlib-devel readline-devel sqlite-devel bzip2-devel bzip2-libs #Mainly install the libraries required for git and python source code compilation and installation

1.2. Download and install pyenv and pyenv-virtualenv
git clone https://github.com/pyenv/pyenv.git ~/pyenv
git clone https://github.com/pyenv/pyenv-virtualenv ~/pyenv-virtualenv
cd ~
mv pyenv-virtualenv/ pyenv/plugins #Move the downloaded pyenv-virtualenv to pyenv/plugins, because pyenv-virtualenv exists as a pyenv plugininsert image description here

1.3. Configure environment variables
echo 'export PYENV_ROOT="$HOME/pyenv"'>> ~/.bashrc

echo ‘export PATH="$PYENV_ROOT/bin:$PATH"’>> ~/.bashrc

echo 'eval “$(pyenv init -)”'>> ~/.bashrc

echo ‘eval “$(pyenv virtualenv-init -)”’ >> ~/.bashrc

source ~/.bashrc #Enable the environment
insert image description here

Execute pyenv, if the output is as shown in the figure below, it means that pyenv is installed successfully
insert image description here

2. Introduction to the use
of pyenv commands Check the available commands through pyenv -h
insert image description here

Commonly used commands are as follows:
pyenv versions # Display the currently used python version
pyenv install --list # List the installable version
pyenv install # Install the corresponding version
pyenv install -v # Install the corresponding version, if an error occurs, you can display detailed error information
pyenv uninstall #Uninstall python
pyenv which python # Display the current python installation path
pyenv global # Set the global python version of the system
pyenv local # Set the python version of the current path, and then enter this directory to automatically switch to this version, with a higher priority than global
pyenv shell # Set the python version of the current shell with a higher priority than global and local
pyenv virtualenv #Create a virtual environment with a version of
pyenv activate #Enter the virtual environment
pyenv deactivate #Exit the virtual environment
pyenv virtuaenvl-delete #Delete the virtual environment
2.1. Install the python version
pyenv install 3.6.7 # Install python 3.6.7
insert image description here

Through the log information, you can know that what the command does is to download the corresponding python version source package first, and then execute the compilation and installation

2.2. View the installed python version
pyenv versions # The python version specified in the current environment by the * code in front of the version
insert image description here

2.3. Specify the python version to use.
There are three commands below to specify
the
python version to use. The version is the version specified by pyenv local, and the priority is higher than the version specified by pyenv global
pyenv shell #Specify the python version of the current shell. After exiting the shell, the specified version will become invalid. The python version specified by this command has the highest priority
: pyenv shell>pyenv local>pyenv global
Experiment 1:
insert image description here

Change the global python version from 2.7.5 to 3.6.7

Experiment 2:
insert image description here

Switch to the /tmp directory and specify the python version used in the directory through pyenv local, which can prove that the priority of pyenv local is indeed greater than that of pyenv global

Experiment three:
insert image description here

Specify the version of the current shell through the pyenv shell. At this time, the version of the current shell is the newly specified version. Even the version under the /tmp directory has been modified, which proves that the priority of the pyenv shell is greater than that of pyenv local. After exiting the current shell, The version specified by pyenv shell will be invalid

3. Create a python virtual environment
With pyenv-virtualenv, we can create multiple different working environments for the same Python interpreter. For example, we create two new working environments with version 3.6.7
pyenv virtualenv 3.6.7 python367-one
pyenv virtualenv 3.6.7 python367-two
insert image description here

pyenv activate #Enter the virtual environment
pyenv deactivate #Exit the virtual environment
pyenv virtualenv-delete #Delete the virtual environment
insert image description here

4. Summary
By using the pyenv and python-virtualenv plug-ins, we can freely switch between different versions. Compared with managing Python versions, it not only saves time, but also avoids mutual interference in the work process.

Guess you like

Origin blog.csdn.net/qq_20663229/article/details/115442219