How to use Pyenv to achieve perfect version control of Python on Linux

Operating system preparation

Just prepare the minimum Linux system.
If you clone in a virtual machine, the MAC address will change.
CentOS 6.5+ is used here

installation

1. Secure git first

yum install git -y

2. Install Python environment dependencies

yum -y install  git  gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel

3. Create a python user

useradd python

4. Install Pyenv after logging in as a python user

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

The downloaded pyenv-installer is a shell script. note:

  1. There is installation documentation at https://github.com/pyenv/pyenv-installer
  2. If curl appears curl: (35) SSL connect error, it is the problem of low nss version, update it. It may be necessary to configure a yum source with a newer package. Add a source in /etc/yum.repo/ as follows, and then update
    [updates]
    name=CentOS-Updates
    baseurl= https://mirrors.aliyun.com/centos/ 6.9/os/x86_64
    gpgcheck=0
    then update nss
    yum update nss

5. Add to the python user's ~/.bash_profile

How to use Pyenv to achieve perfect version control of Python on Linux

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

$ source ~/.bash_profile

In this way, when the user starts, the script in the user's .bash_profile will be executed and pyenv will be started. The installed pyenv is in ~/.pyenv

Use of Pyenv

python version and path

$ python --version
$ python -V
$ echo $PATH

You can see the current system Python path

pyenv command

How to use Pyenv to achieve perfect version control of Python on Linux

$ pyenv help install 
列出所有可用版本 

Install python3.5.3 version online

The installation of $ pyenv versions may be slower. In order to speed up, the cache method is used.
Put the manually downloaded installation package in the ~/.pyenv/cache directory. The
cache directory is a new
specific python version to download. You can download it at https://www.python.org/ftp/python/3.5.3/
PS: Download from the official website. Reliable

Python version control of pyenv

pyenv provides three version control methods, take 3.5.3 as an example

pyenv global 3.5.3  global 全局设置系统为3.5.3  Linux大多基于2.6 和2.7版本,肆意更改,危险系数很大,要谨慎操作!!!
pyenv shell 3.5.3 仅生效于当前回话,一旦断开连接就失效了

下面,是我们平时会经常使用的模式
pyenv local 3.5.3
local 本地设置 使用pyenv local设置从当前工作目录开始向下递归都继承这个设置。

How to use Pyenv to achieve perfect version control of Python on Linux

       As shown in the figure, pyenv can indeed change the python version of the current path, but if the pip management of a version is centralized, it is not suitable for project separation.
This requires the use of pyenv's package separation function

Virtualenv virtual environment settings

       Why use a virtual environment? As mentioned above, if multiple projects are developed with different Python versions, or deployed and run with different Python versions, or developed with the same version but different projects use different versions of the library, these problems will bring Come conflict. The best solution is to run each project independently in its own "independent small environment".

Let's start with a virtual version from version 3.5.3

pyenv virtualenv 3.5.3 mt353 

How to use Pyenv to achieve perfect version control of Python on Linux

       It can exist in the version list, which is the same as 3.5.3, which is a version. The real directory is under ~/.pyenv/versions/. As long as you use this virtual version in the future, the package will go to these corresponding directories instead of using 3.5.3.

Then we go to set the version of a directory,
as shown in the figure, execute pyenv local mt353 according to actual needs. It
How to use Pyenv to achieve perfect version control of Python on Linux
can be seen that the current directory has been loaded with the mt353 version
and related package management will also be integrated under this version.

pip general configuration

       pip is a package management tool for Python, which is directly included in the 3.x version and can be used directly. In order to use domestic mirrors like yun, configure as follows.

Linux system $ mkdir ~/.pip configuration file is in ~/.pip/pip.conf

[global]
index-url=https://mirrors.aliyun.com/pypi/simple/ 
trusted-host=mirrors.aliyun.com

       In different virtual environments, install the redis package and use pip list to see the effect. $ pip -V pip install pkgname command is a command to install python packages that will be used frequently in the future

to sum up

       So far, the use of pyenv to perform python version control and project separation on Centos 6 has been realized, and the python project can be opened freely in the future, any version, any path, pip does not conflict, praise!

Guess you like

Origin blog.51cto.com/14839701/2549996