pyenv python version control

Often encounter such a situation:

The system comes with Python is 2.x, they need Python 3.x, this time need to install multiple systems in Python, but can not affect the system comes with Python, ie the need to implement multiple versions of Python coexistence, pyenv is so a Python version manager.

A: pyenv Introduction

 
 
 
Simply put, pyenv allows users to install and manage multiple versions of Python in the system according to requirements:
  1. Configuration version of the user's current python
  2. The current configuration of python version shell
  3. Configuring an item (directory and subdirectories) version of python
  4. Configuring multiple virtual environments
 II: Introduction Works

pyenv is to use the system PATH environment variable priority, hijacking python command to the pyenv, according to the environment or the user's directory, use different versions of python.
 
    The system for the PATH environment variable, which contains a string of the path separated by a colon, e.g. / usr / local / bin: / usr / bin: / bin. Whenever a command is executed in the system, such as PIP or python, the operating system will find the corresponding command sequence from left to right in all of the PATH. Because it is in turn looking for, so row to the left of the path it has a higher priority. Inserted in front of a $ PATH (pyenv root) / shims directory, $ (pyenv root) / shims directory contains the name and pip python and other executable script file; when the user executes the command python or pip, according to the search priority, the system will give priority to the implementation of the same name script shims directory. pyenv It is through these scripts, the flexibility we need to switch to the version of Python.
More detailed information, you can view the document describes pyenv or source code implementation.
Three: pyenv installation

System Environment: CentOS 6.10
1. Install depend on the environment
 
yum -y installgcc zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel git

 

2. Install the package pyenv
pyenv can be mounted in various ways, you can refer to the item Installtion on github. The automatic installer recommended way to install, can all plug-ins installed pyenv of a button.
 
The automatic installer
Visit my other project: https://github.com/pyenv/pyenv-installer

 

A key installation:
Analyze the above pyenv-installer script can be found on centos, in fact, it did the following things:
 
git clone --depth 1"git://github.com/pyenv/pyenv.git"            "${HOME}/.pyenv"
git clone --depth 1"git://github.com/pyenv/pyenv-doctor.git"     "${HOME}/.pyenv/plugins/pyenv-doctor"
git clone --depth 1"git://github.com/pyenv/pyenv-installer.git"  "${HOME}/.pyenv/plugins/pyenv-installer"
git clone --depth 1"git://github.com/pyenv/pyenv-update.git"     "${HOME}/.pyenv/plugins/pyenv-update"
git clone --depth 1"git://github.com/pyenv/pyenv-virtualenv.git""${HOME}/.pyenv/plugins/pyenv-virtualenv"
git clone --depth 1"git://github.com/pyenv/pyenv-which-ext.git"  "${HOME}/.pyenv/plugins/pyenv-which-ext"

 

3.pyenv installation is complete
After installation, also need to perform the command, the system will be installed pyenv environment variable.
 
 
echo 'export PATH="/home/python/.pyenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
Source ~ / .bash_profile
 # View pyenv version 
pyevn - v 
 # update pyenv 
pyenv Update

 

4: Use of pyenv

pyenv commands, can help to see through pyenv, more detailed information, you can view the document: https://github.com/pyenv/pyenv/blob/master/COMMANDS.md .
 
$ pyenv help
Usage: pyenv <command> [<args>]
 
Some useful pyenv commands are:
   commands    List all available pyenv commands
   local       Set or show the local application-specific Python version
   global      Set or show the global Python version
   shell       Set or show the shell-specific Python version
   install     Install a Python version using python-build
   uninstall   Uninstall a specific Python version
   rehash      Rehash pyenv shims (run this after installing executables)
   version     Show the current Python version and its origin
   versions    List all Python versions available to pyenv
   which       Display the full path to an executable
   whence      List all Python versions that contain the given executable
 
See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme

 

 
1. Installation pyen python
Use pyenv install --list view can install python version, pyenv install <version> installed python, you can use the -v parameter, review the installation process. For more detailed information, use the command python install help understand 
 
$pyenv install --help
Usage: pyenv install [-f] [-kvp] <version>
pyenv install [-f] [-kvp] <definition-file>
pyenv install -l|--list
pyenv install --version
 

-l/--list List all available versions
-f/--force Install even if the version appears to be installed already
-s/--skip-existing Skip if the version appears to be installed already
python-build options:
 

-k/--keep Keep source tree in $PYENV_BUILD_ROOT after installation
(defaults to $PYENV_ROOT/sources)
-p/--patch Apply a patch from stdin before building
-v/--verbose Verbose mode: print compilation status to stdout
--version Show version of python-build
-g/--debug Build a debug version
 

For detailed information on installing Python versions with
python-build, including a list of environment variables for adjusting
compilation, see: https://github.com/pyenv/pyenv#readme
 

$ pyenv install --list
Available versions:
2.1.3
2.2.3
2.3.7
………………
$ pyenv install 3.6.4
 

Downloading Python-3.6.4.tar.xz...
-> https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
Installing Python-3.6.4...
Installed Python-3.6.4 to /home/python/.pyenv/versions/3.6.4
 

$ pyenv install 3.5.4
Downloading Python-3.5.4.tar.xz...
-> https://www.python.org/ftp/python/3.5.4/Python-3.5.4.tar.xz
Installing Python-3.5.4...
Installed Python-3.5.4 to /home/python/.pyenv/versions/3.5.4

 

 2. Check the version of python
Parameters versions, you can view pyenv already installed and can manage python version, preceded by an asterisk (*) indicates the version currently in use; can also be used to view the current version used version
 
# View the version that can be used, preceded by * indicates the currently used version 
pyenv versions
 # View the currently used version 
pyenv version
 
 
 

$ pyenv versions
* system (set by /home/python/.pyenv/version)
3.5.4
3.6.4
$ pyenv version
system (set by /home/python/.pyenv/version)

 

3. Configure and manage python version
pyenv management python versions of three basic commands
  • pyenv global <version>
  • pyenv shelll <version>
  • pyenv local <version>
1) Use pyenv global <version> python configured version of the current user of the system
 
$ pyenv versions
* system
  3.5.4
  3.6.4 (set by /home/python/.pyenv/version)
For python $ - the V
Python 2.6.6
$ pyenv global 3.6.4
$ pyenv versions
  system
  3.5.4
* 3.6.4 (set by /home/python/.pyenv/version)
For python $ - the V
Python 3.6.4 
$ for python - the V
Python 3.6.4
$ exit
logout
# Log back 
$ Python - V
Python 3.6.4

 

2) Use pyenv shelll <version> Configure the current python version of the shell, the shell exits failure
View the current version and the version available python
 
For python $ - the V
Python 2.6.6
 
$ pyenv versions
* system (set by /home/python/.pyenv/version)
  3.5.4
  3.6.4

 

Use pyenv shell <version> command to configure the current python version of the shell
 
$ pyenv shell 3.5.4

 

Check the configuration later version of python
 
For python $ - the V
Python 3.5.4
$ pyenv versions
  system
* 3.5.4 (set by PYENV_VERSION environment variable)
  3.6.4

 

Under the current shell, eliminating the use of python shell --unset configuration; if out of this shell, the configuration will fail.
 
$ pyenv shell --unset

 

3) Use pyenv local <version> configuration where the project (directory) version of python
Create a folder ~ / project, the use of this python local folder <version>
 
$ mkdir project
$ cd project
$ pyenv local 3.6.4

 

In this folder, check the version
 
$ pyenv versions
  system
  3.5.4
* 3.6.4 (set by /home/python/project/.python-version)
 
For python $ - the V
Python 3.6.4

 

New directory ~ / project / p_a, switch to the ~ / project / p_a, and check the version
 
$ mkdir p_a&& cd p_a
 
For python $ - the V
Python 3.6.4
$ pyenv versions
  system
  3.5.4
* 3.6.4 (set by /home/python/project/.python-version)

 

New directory ~ / project / p_b, switch to ~ / project / p_b, using pyenv local system, and check the version
$ mkdir ../p_b&& cd ../p_b
 
$ pyenv local system
For python $ - the V
Python 2.6.6
$ pyenv versions
* system (set by /home/python/project/p_b/.python-version)
  3.5.4
  3.6.4

 

 
4. little deeper look at how pyenv is python version management
Use the command which can be seen, the python command python command is not the original, but in the script file shims
 
$ which python
~/.pyenv/shims/python
$ which python3
~/.pyenv/shims/python3
$ which pip3
~/.pyenv/shims/pip3
 

 

View ~ / .pyenv / shims / python, you can see the python command eventually take over the running ~ / .pyenv / libexec / pyenv, interested students can then see libexec achieve ~ / .pyenv / / pyenv code here not analyzed from the code.
 
 
#!/usr/bin/env bash
set -e
[ -n "$PYENV_DEBUG" ] && set -x
 
program="${0##*/}"
if [[ "$program" = "python"* ]]; then
  for arg; do
    case "$arg" in
    -c* | -- ) break ;;
    */* )
      if [ -f "$arg" ]; then
        export PYENV_FILE_ARG="$arg"
        break
      be
      ;;
    esac
  done
be
 
export PYENV_ROOT="/home/python/.pyenv"
exec "/home/python/.pyenv/libexec/pyenv" exec "$program" "$@"

 

pyenv document, which is so described:
 
 Probably meant to say, when the future use of the python command was taken over pyenv, which use python version in the end, these are in turn determined by the following information:
  • If this variable PYENV_VERSION whether there is used a version of this variable; this variable is configured by pyenv shell <version> of;
  • Look up the root directory, the first one found among .python-version files as a version of the file that specifies the version as the version python order to find the parent directory; This document using pyenv local <version> Configuration
  • $ (Pyenv root) / version If this file exists, then use this file as a developed version of the python version; if not, use the version of the system; this file using pyenv global <version> Configuration
  • If the above variable or documents are not found, according to the system default python version developed by the.
Further, the user can also configure multiple versions of an environment in a python; usage and specific explanation, refer to the documentation.
Look back at the above pyenv versions command, there will be a result output by the prompt set, also demonstrated to the user, pyenv specify what version of python based.
 
Five: python virtual environment

 
In order to isolate different projects, each project uses a separate interpreter and dependence, need to configure the python virtual environment. The machine has been installed pyenv-virtualenv
Use python pyenv install version installed, such as 3.6.4; interpreter installed path ~ / .pyenv / versions / 3.6.4 /; plugins installed is ~ / .pyenv / versions / 3.6.4 / lib / python3.6 / site-packages;
Creating a virtual environment using python pyenv-virtualenv, essentially creating a folder evns, the virtual storage environment interpreter in python ~ / .pyenv / versions / 3.6.4 / case; one and creates ~ / .pyenv / lower soft connection, the virtual environment can be managed by pyenv;
$ pyenv virtualenv 3.6.4 my_3.6.4
Requirement already satisfied: setuptools in /home/python/.pyenv/versions/3.6.4/envs/my_3.6.4/lib/python3.6/site-packages
Requirement already satisfied: pip in /home/python/.pyenv/versions/3.6.4/envs/my_3.6.4/lib/python3.6/site-packages
 
$ll ~/.pyenv/versions/
total 8
drwxr-xr-x 6 python python 4096 Jul 20 00:59 3.5.4
drwxr-xr-x 7 python python 4096 Jul 21 01:03 3.6.4
lrwxrwxrwx 1 python python   48 Jul 21 01:03 my_3.6.4 -> /home/python/.pyenv/versions/3.6.4/envs/my_3.6.4
 
 
View python virtual environment
$ pyenv virtualenvs
  3.6.4/envs/my_3.6.4 (created from /home/python/.pyenv/versions/3.6.4)
  my_3.6.4 (created from /home/python/.pyenv/versions/3.6.4)
 
 
Switch to the python virtual environment
$ pyenv shell my_3.6.4
(my_3.6.4) [python@localhost 3.6.4]$
(my_3.6.4) [python@localhost 3.6.4]$ pyenv versions
  system
  3.5.4
  3.6.4
  3.6.4/envs/my_3.6.4
* my_3.6.4 (set by PYENV_VERSION environment variable)
 
 

 

 

 

 

1. Install pyenv:

1 1.> installation dependencies:
 2  
. 3 yum the install bzip2 zlib-devel-devel-devel OpenSSL the ncurses-devel SQLite the readline-devel devel-TK-gdbm-devel devel devel the libpcap-devel-DB4 an xz-devel - Y
 . 4  
. 5 2 > installation package pyenv:
 . 6  
. 7 Git clone https://github.com/pyenv/pyenv.git ~ / .pyenv
 . 8  
. 9 3.> set the environment variables:
 10  
. 11  # Vim ~ / .bashrc 
12 is Export PYENV_ROOT = " $ the HOME / .pyenv "  
13 is Export the PATH = " $ PYENV_ROOT / bin: $ the PATH "  
14 the eval " $ (pyenv the init -)" 
15  
16  # Source ~ / .bashrc 
17  # That statement is a start, restart the system to execute this statement 
18  Exec $ SHELL

 

2. Check the python version pyenv support, but also test has not been installed correctly:

1
pyenv install  - - list

3. Review all current versions pyenv detectable before the release of active marked with *.

1
2
3
4
[root@localhost ~] # pyenv versions 
   system
   3.5 . 1
*  3.5 . 3  ( set  by  / root / .pyenv / version)

4. Check the version currently active in parentheses indicate the contents of this version is activated by Which pathway (global, local, shell)

1
2
[root@localhost ~] # pyenv version    
3.5 . 3  ( set  by  / root / .pyenv / version)

5. 3.5.1 as global variables, the following command.

1
2
3
[root@localhost ~] # pyenv global 3.5.1
[root@localhost ~] # pyenv version
3.5 . 1  ( set  by  / root / .pyenv / version)

6. The setup for the local version, write the current version number by .python-version files under the directory manner.
 # Create a local directory ops, after performing pyenv local 3.5.3, only the directory is version 3.5.3, another directory using the default versions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost ~] # python -V
Python  3.5 . 1
[root@localhost ~] # pyenv versions
   system
*  3.5 . 1  ( set  by  / root / .pyenv / version)
   3.5 . 3
[root@localhost ~]
[root@localhost ~] # mkdir ops
[root@localhost ~] # cd ops/
[root@localhost ops] # pyenv local 3.5.3
[root@localhost ops] # python -V
Python  3.5 . 3
[root@localhost ops] # cd ..
[root@localhost ~] # python -V
Python  3.5 . 1

7. Install Python versions (such as 3.4.0) you need:

1
2
3
pyenv install  3.4 . 0  - v
 
#小技巧,可以在/root/.pyenv/目录下创建cache目录,将下载好的Python-3.4.0的包放在该目录下,就可以直接执行安装,而不需要下载,节省下载时间.

The need to update the database after the installation is complete:

1
pyenv rehash

9. Uninstall python 3.4.0 version.

1
pyenv uninstall  3.4 . 0

 

Guess you like

Origin www.cnblogs.com/kiko0o0/p/11021560.html