Python multi-version control software under macOS installation: pyenv, pyenv-virtualenv

Software Description :

pyenv, Is a particularly useful version of Python manager, a programmer can create a different directory, run different versions of Python in different directories separately and independently of each other, the installation package also affect each other. github project Address: https://github.com/yyuu/pyenv

pyenv-virtualenvIs pyenva plugin (plugin), can be used to create a clean virtual environment based on different versions of Python. github project Address: https://github.com/yyuu/pyenv-virtualenv

Installation idea :
to install macOS package manager brew, then brewinstall pyenvandpyenv-virtualenv


1. Install brewPackage Manager

brew full name Homebrew , is macOS a package manager under (macOS not have its own package manager), similar to CentOS belowyum, Ubuntu underapt-getcommand.

      
      
1
      
      
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2. Use the brewcommand to install pyenv,pyenv-virtualenv

      
      
1
2
      
      
brew install pyenv
brew install pyenv-virtualenv

Will be installed autoconf, pkg-config, openssl,readline

Installation process, we can see some Caveats (warning), we need to manually handle it.
First link readline to the system lib:

      
      
1
      
      
brew link readline --force

According Caveats then prompts modify environment variables, vim ~/.bash_profileadd the following content:

      
      
1
2
3
4
5
6
      
      
#pyenv settings
export PYENV_ROOT=/usr/local/var/pyenv
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
#pyenv-virtualenv settings
if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi

The above two settings to make pyenvand pyenv-virtualenveasier to use, can be used when the command completion.

Setting up the terminal closed, and then restart the terminal, it can take effect.

3. pyenv to use

**警告:pyenv安装Python是编译安装的,在使用之前要先安装zlibSQLite3,要不然安装会报错。

安装zlibSQLite3并链接:

      
      
1
2
3
4
      
      
brew install zlib
brew install sqlite3
brew link zlib --force
brew link sqlite3 --force

然后根据Caveats的提示修改环境变量,vim ~/.bash_profile添加下面内容:

      
      
1
2
      
      
export PATH= "/usr/local/opt/sqlite/bin:$PATH"

pyenv --help可以查看pyenv的使用帮助:

常用的几个pyenv命令:

pyenv install x.y.z:安装 大专栏  macOS下Python多版本控制软件的安装:pyenv、pyenv-virtualenvPython,x.y.z是Python的版本,如pyenv install 3.6.2

pyenv local x.y.z:设置当前目录的Python版本为x.y.z, 如pyenv local 3.6.2

pyenv versions:查看安装的版本,前面带*号的表示当前目录下正在使用的版本。系统自带的Python是System,后安装的版本的都是版本号

下面给出使用的例子:
示例:

      
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
      
      
$ pyenv install 3.6.2 # 安装3.6.2版本的Python
$ pyenv install 2.7.13 # 安装2.7.13版本的Python
$ pyenv versions # 可以看到3个版本,system为系统自带的版本
* system ( set by /usr/ local/var/pyenv/version)
2.7.13
3.6.2
$ cd # 到家目录
$ mkdir Python36 # 创建Python3.6的工作目录
$ cd Python36
$ pyenv local 3.6.2 # 使当前工作目录使用Python3.6.2版本
$ python -V # 查看一下当前目录用Python的版本,确实是3.6.2
Python3.6.2
$ pip -V # 查看一下pip版本,是3.6的pip
pip 9.0.1 from /usr/ local/var/pyenv/versions/3.6.2/lib/python3.6/site-packages
$ cd # 回到家目录
$ mkdir Python27 # 创建python2.7的工作目录
$ cd Python27
$ pyenv local 2.7.13 # 使当前工作目录使用Python2.7.13版本
$ python -V # 查看一下当前目录用Python的版本,确实是2.7.13
Python 2.7.13
$ pip -V # 查看一下pip版本,是2.7的pip
pip 9.0.1 from /usr/ local/var/pyenv/versions/2.7.13/lib/python2.7/site-packages (python 2.7)

4. pyenv-virtualenv的使用方法

pyenv-virtualenv是用来创建一个干净的虚拟Python环境的命令,通常在创建干净的新项目时候使用。使用方法如下:

1.创建虚拟环境–pyenv virtualenv 版本号 虚拟环境名

      
      
1
      
      
$ pyenv virtualenv 3.6.2 venv-3.6.2
  1. 创建项目,让项目使用干净的Python3.6.2的虚拟环境:
      
      
1
2
3
4
5
6
      
      
[yulongjun@yulongjun ~]$ mkdir Learning-Python3
[yulongjun@yulongjun ~]$ cd Learning-Python3/
[yulongjun@yulongjun Learning-Python3]$ pyenv local venv-3.6.2
(venv-3.6.2) [yulongjun@yulongjun Learning-Python3]$ cd ..
[yulongjun@yulongjun ~]$ cd Learning-Python3/
(venv-3.6.2) [yulongjun@yulongjun Learning-Python3]$

We will find: As long as we enter the Learning-Python3directory, it will automatically activate virtualenv, exit the Learning-Python3directory, it will shut down virtualenv.

If you want to turn off automatic activation, you can run the command pyenv deactivateto re-enable it, run pyenv activate 虚拟环境名.

Guess you like

Origin www.cnblogs.com/wangziqiang123/p/11712106.html