利用virtualenv搭建Python虚拟环境

2017-09-15  本文已影响0人  万越天

1 virtualenv

virtualenv用于创建相互独立的Python环境

安装virtualenv

sudo pip install virtualenv

创建virtualenv目录

wanyongzhendeMacBook-Pro:~ wanyongzhen$ virtualenv testvir
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/wanyongzhen/testvir/bin/python3.6
Also creating executable in /Users/wanyongzhen/testvir/bin/python
Installing setuptools, pip, wheel...done.

切换到virtualenv目录并进入Python虚拟环境

wanyongzhendeMacBook-Pro:~ wanyongzhen$ cd testvir/
wanyongzhendeMacBook-Pro:testvir wanyongzhen$ source bin/activate
(testvir) wanyongzhendeMacBook-Pro:testvir wanyongzhen$ pip list
pip (9.0.1)
setuptools (36.4.0)
wheel (0.29.0)

退出virtualenv Python虚拟环境

(testvir) wanyongzhendeMacBook-Pro:testvir wanyongzhen$ deactivate 

2 virtualenvwrapper

Virtaulenvwrapper是virtualenv的扩展包,用于更方便管理虚拟环境,它可以做:

安装virtualenvwrapper

wanyongzhendeMacBook-Pro:testvir wanyongzhen$ sudo pip install virtualenvwrapper --upgrade --ignore-installed

创建目录用来存放虚拟环境

mkdir ~/.virtualenvs

在.bash_profile中添加

export WORKON_HOME=~/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

生效

source .bash_profile

创建virtualenv

wanyongzhendeMacBook-Pro:~ wanyongzhen$ mkvirtualenv python2.7 --python=python2.7
wanyongzhendeMacBook-Pro:~ wanyongzhen$ mkvirtualenv python3.6 --python=python3.6

命令列表

上一篇 下一篇

猜你喜欢

热点阅读