## 安裝 virtualenv
> 可以用 `pip`、`easy_install` 或者 `pyenv`來安裝virtualenv,但是`推薦pyenv來安裝`,pyenv-virtualenv是pyenv的插件。有助于后面的環境切換等
```
root@pts/5 $ ls -l ~/.pyenv/plugins/
total 4
drwxr-xr-x 5 root root 4096 Jun 7 10:51 python-build
root@pts/5 $ git clone https://github.com/pyenv/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
Cloning into '/root/.pyenv/plugins/pyenv-virtualenv'...
remote: Counting objects: 1800, done.
remote: Total 1800 (delta 0), reused 0 (delta 0), pack-reused 1800
Receiving objects: 100% (1800/1800), 517.45 KiB | 143.00 KiB/s, done.
Resolving deltas: 100% (1230/1230), done.
root@pts/5 $ ls -l ~/.pyenv/plugins/
total 8
drwxr-xr-x 7 root root 4096 Jun 7 12:00 pyenv-virtualenv
drwxr-xr-x 5 root root 4096 Jun 7 10:51 python-build
```
---
## 配置
添加配置 `eval "$(pyenv virtualenv-init -)"` 到 `~/.bashrc `
```
root@pts/5 $ vim ~/.bashrc
root@pts/5 $ cat ~/.bashrc |egrep 'virtualenv'
eval "$(pyenv virtualenv-init -)"
```
---
## 配置生效
### 方式一
重新打開一個session
### 方式二
source ~/.bashrc
### 方式三
exec $SHELL -l
---
## 創建虛擬環境
> 創建新的虛擬環境實際存在于`~/.pyenv/versions/`目錄
```
## 創建 名字為py3.4.3的虛擬環境
## 注意 第三個 '3.4.4' 版本號碼是必須的
root@pts/5 $ pyenv virtualenv 3.4.4 py3.4.4
Ignoring indexes: https://pypi.python.org/simple
Requirement already satisfied (use --upgrade to upgrade): setuptools in /root/.pyenv/versions/3.4.4/envs/py3.4.4/lib/python3.4/site-packages
Requirement already satisfied (use --upgrade to upgrade): pip in /root/.pyenv/versions/3.4.4/envs/py3.4.4/lib/python3.4/site-packages
## 查看新增的虛擬環境
root@pts/5 $ ls -l ~/.pyenv/versions/
total 8
drwxr-xr-x 6 root root 4096 Jun 7 11:43 2.7.11
drwxr-xr-x 7 root root 4096 Jun 7 12:05 3.4.4
lrwxrwxrwx 1 root root 40 Jun 7 12:05 py3.4.4 -> /root/.pyenv/versions/3.4.4/envs/py3.4.4
root@pts/5 $ pyenv versions
system
2.7.11
* 3.4.4 (set by /root/.pyenv/version)
3.4.4/envs/py3.4.4
py3.4.4
```
---
## 虛擬環境使用
```
## 進入虛擬環境
root@pts/6 $ pyenv activate py3.4.4
pyenv-virtualenv: prompt changing will be removed from future release. configure 'export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
(py3.4.4) Dev-mysql-mem [~] 2016-06-07 12:12:35
## 查看虛擬環境版本
root@pts/6 $ python -V
Python 3.4.4
## 退出到系統環境
root@pts/6 $ pyenv deactivate
Dev-mysql-mem [~] 2016-06-07 12:14:46
root@pts/6 $
```
---
## 卸載虛擬環境
```
## 方式一:暴力刪除
rm -rf ~/.pyenv/versions/py3.4.4
## 方式二:溫柔卸載
pyenv uninstall py3.4.4
```
## Refer to
> [pyenv插件virtualenv](https://github.com/yyuu/pyenv-virtualenv)