pyenv虛擬環境安裝

安裝過程

配置yum源

# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo


# yum -y install yum-utils

# yum install wget -y 

# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 

安裝git

# yum install -y git

安裝Python環境依賴

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

添加用戶

# useradd python

# echo 132456|passwd --stdin python

# su - python

安裝pyenv

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

添加環境變數

$ vim .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/.local/bin:$HOME/bin

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


$ . .bash_profile

查看python版本

$ python -V
Python 2.7.5

 Pyenv的命令

$ pyenv 
pyenv 1.2.20
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   activate    Activate virtual environment
   commands    List all available pyenv commands
   deactivate   Deactivate virtual environment
   doctor      Verify pyenv installation and development tools to build pythons.
   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
   virtualenv   Create a Python virtualenv using the pyenv-virtualenv plugin
   virtualenv-delete   Uninstall a specific Python virtualenv
   virtualenv-init   Configure the shell environment for pyenv-virtualenv
   virtualenv-prefix   Display real_prefix for a Python virtualenv version
   virtualenvs   List all Python virtualenvs found in `$PYENV_ROOT/versions/*'.
   whence      List all Python versions that contain the given executable
   which       Display the full path to an executable

虛擬環境下安裝Python3.5.3版本

1、從官網下載(非常慢,不建議)

$ pyenv install 3.5.3 -v
/tmp/python-build.20200718063111.6245 ~
Downloading Python-3.5.3.tar.xz...
-> https://www.python.org/ftp/python/3.5.3/Python-3.5.3.tar.xz

2、傳包的方式

$ ls -a
.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc  .pki  .pyenv  .viminfo

$ mkdir .pyenv/cache

將下載好的包放入cache目錄,然後執行安裝命令

$ pyenv install 3.6.4 -v
/tmp/python-build.20200718070338.7001 ~/.pyenv
/tmp/python-build.20200718070338.7001/Python-3.6.4 /tmp/python-build.20200718070338.7001 ~/.pyenv
Installing Python-3.6.4...
checking build system type... x86_64-pc-linux-gnu

pyenv版本控制

當前登錄用戶全局生效

[python@localhost .pyenv]$ pyenv version
system (set by /home/python/.pyenv/version)

[python@localhost .pyenv]$ python -V
Python 2.7.5

更改全局版本(企業中禁用)

$ pyenv version
3.6.4 (set by /home/python/.pyenv/version)

$ python -V
Python 2.7.5

打開新終端查看版本資訊

[python@localhost ~]$ pyenv versions
  system
* 3.6.4 (set by /home/python/.pyenv/version)

[python@localhost ~]$ python -V
Python 3.6.4

切回原版本

[python@localhost ~]$ pyenv global system

[python@localhost ~]$ pyenv versions
* system (set by /home/python/.pyenv/version)
  3.6.4

注意:這裡的global參數作用的是python用戶,而非root用戶。如果是root用戶安裝的,那麼不要用global,影響很大

修改當前shell的Python版本

[python@localhost .pyenv]$ pyenv shell 3.6.4

[python@localhost .pyenv]$ pyenv versions
  system
* 3.6.4 (set by PYENV_VERSION environment variable)

[python@localhost .pyenv]$ python -V
Python 2.7.5

新終端查看版本

[python@localhost ~]$ pyenv versions
* system (set by /home/python/.pyenv/version)
  3.6.4

[python@localhost
~]$ python -V Python 2.7.5
這種方法是當前shell生效的,如果當前shell關閉了,版本設置就無效了,再打開shell的時候版本就變回原來的了,還需要重新再設置一次,非常麻煩,推薦使用下面的方法

Local(當前文件夾生效)

創建項目目錄

[python@localhost .pyenv]$ cd
[python@localhost ~]$ mkdir zh/projects -p
[python@localhost ~]$ cd zh/projects/

新終端切換到項目目錄

[python@localhost ~]$ cd zh/projects/

新終端設置當前目錄版本

[python@localhost projects]$ pyenv local 3.6.4

[python@localhost projects]$ pyenv versions
  system
* 3.6.4 (set by /home/python/zh/projects/.python-version)

[python@localhost projects]$ python -V
Python 3.6.4

查看其它目錄版本

[python@localhost cmdb]$ cd

[python@localhost ~]$ pyenv versions
* system (set by /home/python/.pyenv/version)
  3.6.4

[python@localhost ~]$ python -V
Python 2.7.5

查看子目錄版本

[python@localhost projects]$ mkdir cmdb

[python@localhost projects]$ cd zh/projects/cmdb

[python@localhost cmdb]$ pyenv versions
  system
* 3.6.4 (set by /home/python/zh/projects/.python-version)

[python@localhost cmdb]$ python -V
Python 3.6.4

虛擬環境(解決打包問題)

安裝3.5.3版本

[python@localhost ~]$ cd .pyenv/cache/

[python@localhost cache]$ ll
total 31456
-rw-r--r--. 1 python python 15213396 Dec  7  2018 Python-3.5.3.tar.xz
-rw-r--r--. 1 python python 16992824 Nov 17  2018 Python-3.6.4.tar.xz

[python@localhost projects]$ pyenv install 3.5.3 -v

[python@localhost ~]$ ll .pyenv/versions
total 0
drwxr-xr-x. 6 python python 56 Jul 18 08:39 3.5.3
drwxr-xr-x. 6 python python 56 Jul 18 07:34 3.6.4

創建虛擬環境

[python@localhost cmdb]$ pyenv virtualenv 3.5.3 zh353
Requirement already satisfied: setuptools in /home/python/.pyenv/versions/3.5.3/envs/zh353/lib/python3.5/site-packages
Requirement already satisfied: pip in /home/python/.pyenv/versions/3.5.3/envs/zh353/lib/python3.5/site-packages

查看版本資訊

[python@localhost cmdb]$ pyenv versions
  system
  3.5.3
  3.5.3/envs/zh353
* 3.6.4 (set by /home/python/zh/projects/.python-version)
  zh353

修改當前項目目錄的Python版本

[python@localhost cmdb]$ pyenv local zh353 

(zh353) [python@localhost cmdb]$ pyenv versions
  system
  3.5.3
  3.5.3/envs/zh353
  3.6.4
* zh353 (set by /home/python/zh/projects/cmdb/.python-version)

創建新的項目目錄並設置版本

(zh353) [python@localhost cmdb]$ mkdir ../web
(zh353) [python@localhost cmdb]$ cd ../web

[python@localhost web]$ pyenv local 3.6.4
[python@localhost web]$ pyenv versions
  system
  3.5.3
  3.5.3/envs/zh353
* 3.6.4 (set by /home/python/zh/projects/web/.python-version)
  zh353