Linux system Python virtual environment management software pyenv

When learning Python, the question most frequently encountered is the package version of the problem, such as the application requires version 1.1.1 libtool package, and application development before you can only run on version 1.0.1. So how to make two applications can be up and running normal? Smart developers have proposed the concept of a virtual environment, like a computer as a virtual machine, create a virtual environment created as a virtual machine. You do anything in the virtual environment will not affect other virtual environments, it will not affect the system environment. You also can install any version of the package in your virtual environment for testing.
In yesterday's article introduces more of a virtual environment management pack virtualenv Python use this package for use on windows systems more. In the Linux / Unix systems use more of a virtual environment is another management tool pyenv.

What is pyenv

pyenv is a virtual environment management tool that can manage multiple versions of Python, and can create different virtual environments to the Python version as a template, and the flexibility to switch between these virtual environments. It has the following advantages:

  • You can directly modify global Python version (careful operation, dangerous)
  • Each project a version of Python
  • Python versions simultaneously from multiple search commands, to facilitate testing across versions of software.

Since so convenient, so we look at it is how to use it.

installation

This software does not currently support the use of the system on the windows, only supports installation on Linux and Unix. For macOS, can be used to install the Homebrew, corresponding official document link is: https://github.com/pyenv/pyenv#homebrew-on-macos
for Linux / Unix users, there are two installation methods, the first species is a step by step installation according to official documents, the steps are as follows:

  1. Cloning software installation. Command is

    $ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
  2. Define environment variables

    $ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
    $ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile

    For zsh user, we need to ~/.bash_profilechange ~/.zshrc.
    For users with Ubuntu, we need to ~/.bash_profilechange~/.bashrc

  3. Adding pyenv init command to the shell enabled shims and auto-completion, shims is a tool for selecting multiple versions of Python, add the following command:

    $ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

    For zsh user, we need to ~/.bash_profilechange ~/.zshrc.
    For users with Ubuntu, we need to ~/.bash_profilechange~/.bashrc

  4. Let configuration command to restart the shell effect, you can log in again to exit the shell, you can execute the command:
    $ exec "$SHELL"

Installation steps above look very complicated, right, official and therefore provides a one-click installation script, by the one-click installation script, the above three steps can be omitted, the installation script command is:

$ curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash

Note: You need to install in advance git, git because it is the first by cloning locally.

Script execution is as follows:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   148  100   148    0     0     20      0  0:00:07  0:00:07 --:--:--    39
100  2454  100  2454    0     0     94      0  0:00:26  0:00:26 --:--:--   668
Cloning into '/root/.pyenv'...
remote: Enumerating objects: 670, done.
remote: Counting objects: 100% (670/670), done.
remote: Compressing objects: 100% (503/503), done.
remote: Total 670 (delta 335), reused 255 (delta 76), pack-reused 0
Receiving objects: 100% (670/670), 380.59 KiB | 30.00 KiB/s, done.
Resolving deltas: 100% (335/335), done.
....
Cloning into '/root/.pyenv/plugins/pyenv-which-ext'...
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 10 (delta 1), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (10/10), done.

This intermediate can clone multiple packets, output is relatively long, similar content, a portion is omitted here. After the script is finished, you will be prompted, pyenv not added to the PATH, then you add it based on its tips. Prompt content as follows:

WARNING: seems you still have not added 'pyenv' to the load path.

# Load pyenv automatically by adding
# the following to ~/.bashrc:

export PATH="/root/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Operation we need to do is open the ~/.bashrcfile, add into the bottom three lines, add a good ~/.bashrcfile looks like this:

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
export PATH="/root/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Save and exit, and then execute the following command:

source ~/.bashrc

Let configuration to take effect.

use

Here, pyenv has been installed. We then execute the command pyenv versions:

[root@adminnode ~]# pyenv versions 
* system (set by /root/.pyenv/version)

This command is not view the version pyenv, but rather to see which of several versions of Python currently installed on the system, under normal circumstances, only just installed a system, corresponds to the python version of the system. For example CentOS7 default python2.7, while CentOS 8 is now defaulted python3.6.

So if we want to use as a template for other versions of the python virtual environment, you need to install their own corresponding versions, such as the latest version of Python now has reached version 3.8.2, and I hope that this version as a template. Then I need to install 3.8.2.

  1. Install dependencies required to compile python

    yum install -y gcc make patch gdbm-devel openssl-devel sqlite-devel \
    readline-devel zlib-devel bzip2-devel  libffi-devel
  2. Download python source package
    this step if it is on the home server or virtual machine recommended action, because we ourselves do not download, then, pyenv it default to python.org official website to download the source package, because the network environment in different regions is not the same, this step may result in particularly slow, or even directly to fail. Therefore, we recommend first of all to python own official website to download a good source package. And upload it to your server, and then copied to pyenv cache directory, as follows:
    
    [root@adminnode ~]# ls
    anaconda-ks.cfg   Python-3.8.2.tar.xz

[adminnode to set a root @ ~] # mkdir ~ / pyenv / cache
[to set a root @ adminnode ~] #
[to set a root @ adminnode ~] # cp Python-382tarxz ~ / pyenv / cache /

然后再执行安装命令的时候,它就会自动使用缓存里的源码包,这样安装速度就会比较快。

3. 安装

[root@adminnode ~]# pyenv install 3.8.2
Installing Python-3.8.2...
Installed Python-3.8.2 to /root/.pyenv/versions/3.8.2

耐心等待它安装完成,这里安装命令后面直接跟上python对应的版本号即可。安装完成后,所有版本的python都是安装在/root/.pyenv/versions目录下,如下所示:

[root@adminnode ~]# ls /root/.pyenv/versions/
3.8.2

然后我们再查看已经安装的版本信息,就能够看到新安装的3.8.2,如下所示:

[root@adminnode ~]# pyenv versions

  • system (set by /root/.pyenv/version)
    3.8.2
  1. use

Since the template to build a good, then we can now use this template to create a virtual environment we need. Before creating our need to say a few points to note:

  • The current template is installed in a package, then the virtual environment to this template will be created in this package.
  • After creating a virtual environment, the newly installed template package does not appear in the virtual environment. That virtual environment is created, templates and virtual environment is completely isolated environment of the two.

Let's look at a virtual environment to create specific example, to create a virtual environment basic format of the command is:

pyenv virtualenv  template_version   virtualenv_name

template_version refers to the version number of the template, virutalenv_name term refers to the virtual environment, usually the name of your project. Suppose we want to develop a cmdb project, then we cmdb to create a virtual environment, as follows:

[root@adminnode ~]# pyenv virtualenv 3.8.2 cmdb
Looking in links: /tmp/tmp_42mjdiv
Requirement already satisfied: setuptools in /root/.pyenv/versions/3.8.2/envs/cmdb/lib/python3.8/site-packages (41.2.0)
Requirement already satisfied: pip in /root/.pyenv/versions/3.8.2/envs/cmdb/lib/python3.8/site-packages (19.2.3)
[root@adminnode ~]# pyenv versions
* system (set by /root/.pyenv/version)
  3.8.2
  3.8.2/envs/cmdb
  cmdb

This time you can see the virtual environment to create a good name, but you can also see the python version or system in the current directory, how to switch to cmdb version of it?

pyenv is the directory for version control units, for example, you have a single project project directory, I set a good python version of the project in the project directory, subdirectory of the project will automatically inherit all this configuration. Set directory python version of the command is:

pyenv local virtualenv_name

Examples are as follows:

[root@adminnode ~]# mkdir cmdb
[root@adminnode ~]# ls
anaconda-ks.cfg  cmdb  docker  Python-3.8.2.tar.xz
[root@adminnode ~]# cd cmdb
[root@adminnode cmdb]# pyenv local cmdb
(cmdb) [root@adminnode cmdb]# 

We can see in the above code, switch to the next item after cmdb directory, set the directory virtual environment cmdb, then we can see from the shell prompt in front of one (cmdb) prompt, indicate we entered into the virtual environment, that is to say at this time is an independent python development environment. We look at the contents of this directory, as follows:

(cmdb) [root@adminnode cmdb]# ls -al 
total 8
drwxr-xr-x. 2 root root   29 Mar  3 11:29 .
dr-xr-x---. 9 root root 4096 Mar  3 11:24 ..
-rw-r--r--. 1 root root    5 Mar  3 11:29 .python-version
(cmdb) [root@adminnode cmdb]# cat .python-version 
cmdb

You can see more than a hidden file .python-version, the file content is cmdb, that is to say pyenv through this virtual environment to control file in the current directory.

  1. To delete a virtual environment

As more and more development projects in the future, it may be a virtual information environment will be more and more, so you need to remove some of the virtual environment is no longer used, this time you need to use the Delete command, as follows:

pyenv virtualenv-delete virtualenv_name

But before deleting, it is recommended that you export the list of packages in the virtual environment through a virtual environment pip command, the command is:

pip freeze --all  > requirements.txt

The installed package requirements.txt exported to a file, then delete the virtual environment:

(cmdb) [root@adminnode cmdb]# pyenv virtualenv-delete cmdb
pyenv-virtualenv: remove /root/.pyenv/versions/3.8.2/envs/cmdb? y
[root@adminnode cmdb]# ls -al 
total 12
drwxr-xr-x. 2 root root   53 Mar  3 11:41 .
dr-xr-x---. 9 root root 4096 Mar  3 11:24 ..
-rw-r--r--. 1 root root    5 Mar  3 11:29 .python-version
-rw-r--r--. 1 root root   31 Mar  3 11:41 requirements.txt
[root@adminnode cmdb]# cat .python-version 
cmdb

When removal is complete, .python-version files in the current directory under there, but because the virtual environment can not find the corresponding information, then it can not take effect.

These are pyenv configuration and use basic information if interested can leave a message for the content we can discuss.

Guess you like

Origin blog.51cto.com/14730652/2475091