×
广告

Centos 下 python 纯净虚拟环境

96
全栈运维
2016.06.07 14:37* 字数 182

[toc]


Centos 下 python 纯净虚拟环境

实际python开发中,可能并行的有多个python项目的开发,他们用到的python版本可能不一样,这个时候就需要有多个相互不依赖的环境来运行多个python项目virtualenv 就刚好满足这个需求


安装 virtualenv

可以用 pipeasy_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/yyuu/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

配置

root@pts/5 $ vim ~/.bashrc 
Dev-mysql-mem [~] 2016-06-07 12:01:08
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


----------------------------------------------更多精彩请关注-------------------------------------

公众号: Colinws工作室
Python
Web note ad 1