Installation and simple use of pyenv (git, pyenv, pyenv-virtualenv)

Installation and simple use of pyenv (git, pyenv, pyenv-virtualenv)

Python work environment management

There is a big difference between Python 2 and Python 3, and due to various reasons, Python 2 and Python 3 coexist for a long time. In the actual work process, we may use Python 2 and Python 3 at the same time, therefore, we need to frequently switch back and forth between Python 2 and Python 3. In addition, if you are someone who likes early adopters, then you are likely to download the Python version immediately when the new version of Python comes out and experiment with the features of Python.

In the Python world, in addition to managing the Python version, you also need to manage different software packages. In most cases, we can use the version for open source libraries. However, sometimes it may be necessary to use different versions of software packages in different projects for the same Python version.

In this section, we will introduce two tools, pyenv and virtualenv. The former is used to manage different Python versions, and the latter is used to manage different working environments. With these two tools, Python-related version issues will no longer be a problem.

1. Problem scenario

  1. The Python interpreter version is confusing, 2 and 3 are very different, and the subdivided versions are also different, which is difficult to select and manage.
  2. Different Linux distributions come with different Pythons. For example, ubuntu16 comes with 2.7 and 3.5 versions. Many components of the system depend on their own interpreters. Once deleted or changed, they may cause problems in the system.
  3. Different Python interpreter package management is also a problem, such as necessary package components such as pip and ipython, and how to ensure that different package environments do not interfere with each other in project development is also a problem.

So is there an ultimate solution to control different package environments while managing different interpreter versions? Yes, it is pyenv.

2. Use pyenv to manage different Python versions

Installing different Python versions is not an easy task, switching back and forth between different Python versions is more difficult, and coexistence of multiple versions is very easy to interfere with each other. Therefore, we need a tool called pyenv. pyenv is a Python version management tool, it can switch the global Python version, and can also provide the corresponding Python version for a single project. After using pyenv, you can install multiple different Python versions on the server, or you can install different Python implementations. Switching between different Python versions is also very simple. Next, let's take a look at the installation and use of pyenv.

3. What is pyenv? What can it do?

Pyenv is a simple, low-key, Python environment management tool forked from the Ruby community. It can easily switch the version of the global interpreter.At the same time, it can easily manage the corresponding package source in combination with the vitualenv plugin.

We know that when entering a command such as' ls' in the terminal, the shell will see from each directory in the PATH of the current environment whether there is an executable file of ls, and execute it if found, otherwise it will report 'command no found 'Error, for the same reason, as long as the PATH variable is controlled, the python version can be switched. Pyenv controls the python version by inserting the shims path in the PATH header.

Installation and simple use of pyenv (git, pyenv, pyenv-virtualenv)

The relationship between pyenv and popular pipenv and virtualenv

Pipenv is a python virtual environment management tool written by the author of requests, Kenneth Reitz, which combines the functions of pip and virtualenv.The focus is still on the package environment management. The idea is to first create an environment that specifies the Python version, and then on this environment. Install the corresponding package, good reviews, see many Daniel are recommended.

Virtualenv is a more traditional and mature virtual environment management tool, and more people are used. The idea is to create a virtual environment, and then install the corresponding package. To enter the environment, activate the activation script and activate it. Although mature, I personally do not I like to use it too. When deploying a project, I always have some environmental problems.

Pyenv is relatively poor in popularity, but it is also very stable. I have used all three environmental management tools. I personally prefer pyenv for the following reasons:

  • Compared with the other two tools, pyenv focuses more on the version management of the Python interpreter, which is a larger level than the package management. Using pyenv, I can easily download the specified version of the Python interpreter, pypy, anaconda, etc. Python interpreter for local and global switching in shell environment
  • There is no need to restrict a certain version of the virtual environment during development, just specify a certain version with pyenv during deployment
  • When pyenv switches the interpreter version, pip and ipython and the corresponding package environment are switched together, so it is convenient if you want to run ipython2.x and ipython3.x multiple interpreters to verify some code
  • pyenv can also create a specified virtual environment, but there is no need to specify a specific directory, the degree of freedom is higher, and the use is also simple

4. Simple to use

# 查看当前版本
pyenv version

# 查看所有版本
pyenv versions

# 查看所有可安装的版本
pyenv install --list

# 安装指定版本
pyenv install 3.6.5
# 安装新版本后rehash一下
pyenv rehash

# 删除指定版本
pyenv uninstall 3.5.2

# 指定全局版本
pyenv global 3.6.5

# 指定多个全局版本, 3版本优先
pyenv global 3.6.5 2.7.14

# 实际上当你切换版本后, 相应的pip和包仓库都是会自动切换过去的

One, pyenv installation

linux environment

version Host surroundings
centos7.6 192.168.1.80 python3

1. Install git

[root@python ~]# yum install git

2. Open the terminal

This article uses bash

3. Install pyenv

Note: All installations in this article strictly abide by the official documents and are completely consistent with the official documents. \

git address: https://github.com/pyenv/pyenv

Run the following command in your terminal, it is safe and non-toxic, please rest assured to eat:

First clone the project and put it in a hidden folder under the home directory: .pyenv

git clone https://github.com/pyenv/pyenv.git ~/.pyenv

Then configure the environment variables

If you use bash, execute the following commands in order:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc
If you use zsh, execute the following commands in order:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.zshrc

The meaning of the echo command is: write the content in quotation marks to a file.
Please note that the last long command of the above three echo commands, please ensure that the content in quotation marks is in ~ / .bashrc or ~ / .zshrc The bottom.
Because the path environment variable is manipulated during pyenv initialization, resulting in unpredictable behavior.
To view the bottom of the file, you can use the tail command, usage: tail ~ / .bashrc or tail ~ / .zshrc, edit the file can use vim or vscode

Finally, before using pyenv, re-initialize the shell environment and execute the following command

exec $SHELL

It is entirely possible not to execute this command. You can close the current terminal window and restart one.

At this point, you have completed the installation of pyenv, you can use all of its commands, but I suggest you do n’t use it in a hurry, install a plug-in of pyenv in one breath, that is pyenv-virtualenv

4. Install pyenv-virtualenv

virtualenv itself is an independent project to isolate the working environment of different projects. For example, user lmx wants to use Flask 0.8 in project A, and at the same time, wants to use Flask 0.9 in project B. If we install Flask globally, it will inevitably fail to meet the needs of users. At this time, we can use virtualenv.

Readers need to pay attention to the difference between pyenv and virtualenv. pyenv is used to manage different Python versions, for example, your system uses Python 2.7.13 when working, and Python 3.6.0 when learning. Virtualenv is used to isolate the working environment of the project. For example, both Project A and Project B use Python 2.7.13. However, Project A needs to use Flask version 0.8 and Project B needs to use Flask version 0.9. As long as we combine the two tools of pyenv and virtualenv, we can construct any combination of versions of Python and third-party libraries, which has good flexibility and avoids mutual interference between projects.

Virtualenv itself is an independent tool, users can use virtualenv alone without using pyenv. However, if you use pyenv, you need to install the pyenv-virtualenv plug-in instead of using the virtualenv function through the virtualenv software.

git address: https://github.com/pyenv/pyenv-virtualenv

(1) Clone the plugin in the plugins folder of pyenv that has just been installed

git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv

(2) Then configure the environment variables

If you use bash, execute the following command:
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
If you use zsh, execute the following command:
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.zshrc

(3) Finally, before using pyenv, re-initialize the shell environment and execute the following command

exec $SHELL

It is entirely possible not to execute this command. You can close the current terminal window and restart one.

At this point, pyenv is installed. We can verify whether pyenv is installed correctly and obtain help information for pyenv by the following command:

[root@python ~]# pyenv --help
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   commands    List all available pyenv commands
   exec        Run an executable with the selected Python version
   global      Set or show the global Python version(s)
   help        Display help for a command
   hooks       List hook scripts for a given pyenv command
   init        Configure the shell environment for pyenv
   install     Install a Python version using python-build
   local       Set or show the local application-specific Python version(s)
   prefix      Display prefix for a Python version
   rehash      Rehash pyenv shims (run this after installing executables)
   root        Display the root directory where versions and shims are kept
   shell       Set or show the shell-specific Python version
   shims       List existing pyenv shims
   uninstall   Uninstall a specific Python version
   version     Show the current Python version(s) and its origin
   --version   Display the version of pyenv
   version-file   Detect the file that sets the current pyenv version
   version-name   Show the current Python version
   version-origin   Explain how the current Python version is set
   versions    List all Python versions available to pyenv
   whence      List all Python versions that contain the given executable
   which       Display the full path to an executable

See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme

Second, use pyenv

Only the daily usage of pyenv and virtualenv is shown here \

1. Check if the installation is correct

Check the version of pyenv
[root@python ~]# pyenv version
(set by /root/.pyenv/version)
See which python versions pyenv has managed
[root@python ~]#pyenv versions
* system (set by /root/.pyenv/version)

If you see the normal version information, it means OK. If you see something like command not found, it means the installation has failed.

We can view which Python versions currently supported by pyenv through the install command of pyenv, as follows:

[root@python ~]# pyenv install --list 
Available versions:
2.1.3
……省略部分信息
3.8.0
3.8-dev
3.8.1
3.9-dev
……省略部分信息
anaconda3-2018.12
anaconda3-2019.03
anaconda3-2019.07
anaconda3-2019.10
……省略部分信息
[root@python ~]#

2, pyenv switch python version

Because the list of Python versions that pyenv can install is very long, it has been omitted here. Readers can install pyenv on their computers, and then execute the pyenv install --list command to view. As you can see, pyenv can not only install different Python versions, but also install different Python implementations, and also install versions of Python for learning.

View the Python version included in the current system:

[root@python ~]# pyenv versions 
* system (set by /root/.pyenv/version)
Use pyenv to install different Python versions:
[root@python ~]#pyenv install -v 3.8.1  
[root@python ~]#pyenv install -v 2.7.13 
Check the Python version included in the current system again
[root@python ~]# pyenv versions
* system (set by /root/.pyenv/version)
  2.7.13
  3.8.1

Switch version

#切换前为3.8.1
[root@python ~]# python
Python 3.8.1 (default, Apr 20 2020, 15:00:10) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

#切换为2.7.13
[root@python ~]# pyenv  global 2.7.13 
[root@python ~]# python
Python 2.7.13 (default, Apr 20 2020, 15:04:15) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

After using pyenv, you can quickly switch the Python version. After switching the Python version, the version-dependent dependencies will also be switched together. Therefore, we do not have to worry about whether different versions will interfere with each other in the system. For example, after switching the Python version, the corresponding pip will also switch, so don't worry about the mismatch between the pip version and the Python version you use, as follows:

[root@python ~]# pyenv  global 3.8.1
[root@python ~]# pip --version
pip 19.2.3 from /root/.pyenv/versions/3.8.1/lib/python3.8/site-packages/pip (python 3.8)
If you want to delete the Python version, use the uninstall command. As follows:
[root@python ~]# pyenv uninstall 2.7.10

Third, the use of pyenv-virtualenv

With pyenv-virtualenv, we can create multiple different working environments for the same Python interpreter. For example, we create two new working environments:

[root@python ~]# pyenv virtualenv 3.8.1 first_project 
[root@python ~]# pyenv virtualenv 3.8.1 second_projec
You can use the virtualenvs subcommand to view the working environment
[root@python ~]# pyenv virtualenvs
  3.8.1/envs/first_project (created from /root/.pyenv/versions/3.8.1)
  3.8.1/envs/second_projec (created from /root/.pyenv/versions/3.8.1)
  first_project (created from /root/.pyenv/versions/3.8.1)
  second_projec (created from /root/.pyenv/versions/3.8.1)

After creating a working environment, you can enter or exit a working environment through the activate and deactivate subcommands. After entering the working environment, the prompt on the left will display your current working environment to avoid operating errors due to too many environments.

(first_project) [root@python ~]# pip install flask==1.1.1 
Looking in indexes: https://pypi.doubanio.com/simple
Collecting flask==1.1.1
  Downloading https://pypi.doubanio.com/packages/9b/93/628509b8d5dc749656a9641f4caf13540e2cdec85276964ff8f43bbb1d3b/Flask-1.1.1-py2.py3-none-any.whl (94kB)
     |███▌                            | 10kB 28.0MB/s eta 0:00:
     |███████                         | 20kB 1.8MB/s eta 0:00:0
     |██████████▍                     | 30kB 2.7MB/s eta 0:00:0
     |█████████████▉                  | 40kB 1.8MB/s eta 0:00:0
     |█████████████████▍              | 51kB 1.3MB/s eta 0:00:0
     |████████████████████▉           | 61kB 1.5MB/s eta 0:00:0
     |████████████████████████▎       | 71kB 1.4MB/s eta 0:00:0
     |███████████████████████████▊    | 81kB 1.3MB/s eta 0:00:0
     |███████████████████████████████▏| 92kB 1.4MB/s eta 0:00:0
     |████████████████████████████████| 102kB 1.6MB/s 
Collecting itsdangerous>=0.24 (from flask==1.1.1)
(first_project) [root@python ~]# pyenv deactivate
##退出first_project环境
[root@python ~]# 

Next, let's take a look at installing different Flask versions in different working environments

[root@python ~]# pyenv activate first_project 
##切换工作环境
(first_project) [root@python ~]# pip install flask==1.1.1 
##安装1.1.1的flask
(first_project) [root@python ~]# pyenv deactivate 
##退出目前工作环境
[root@python ~]#

[root@python ~]# pyenv activate second_projec
##切换工作环境
(second_project) [root@python ~]# pip install flask==0.10.1
##安装0.10.1的flask
(second_project) [root@python ~]# pyenv deactivate 
##退出目前工作环境
[root@python ~]#
Look at the source directory of the two working environments

Installation and simple use of pyenv (git, pyenv, pyenv-virtualenv)

If you want to delete the virtual environment, use:

(first_project) [root@python ~]# pyenv virtualenv-delete first_project 

Using the pyenv and python-virtualenv plugins, we can freely switch between different versions. Compared to managing Python versions, not only saves time, but also avoids mutual interference in the work process.

Three, update pyenv

Since we are git cloned, the update is very simple

cd ~/.pyenv` 或者 `cd $(pyenv root)`
 `git pull

Four, uninstall pyenv

Since pyenv put everything under ~ / .pyenv, it is very convenient to uninstall, just two steps

First you need to delete the environment variable

Then you need to execute:

rm -rf ~/.pyenv` 或者 `rm -rf $(pyenv root)

Guess you like

Origin blog.51cto.com/14320361/2488888