virtualenv搭建Python虛擬環境

virtualenv

參考:https://ilaotan.github.io/2015/08/24/python-virtualEnv-pycharm.htmlhtml

virtualenv用於建立相互獨立的Python環境python

  • 使不一樣應用開發環境獨立
  • 環境升級不影響其餘應用,也不會影響全局的Python環境
  • 它能夠防止系統中出現包管理混亂和版本的衝突

安裝virtualenvgit

sudo pip install virtualenv

建立virtualenv目錄github

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虛擬環境bash

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虛擬環境app

(testvir) wanyongzhendeMacBook-Pro:testvir wanyongzhen$ deactivate

virtualenvwrapper

參考:http://www.jianshu.com/p/3abe52adfa2bpython2.7

Virtaulenvwrapper是virtualenv的擴展包,用於更方便管理虛擬環境,它能夠作:code

  • 將全部虛擬環境整合在一個目錄下
  • 管理(新增,刪除,複製)虛擬環境
  • 快速切換虛擬環境

安裝virtualenvwrapperhtm

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

建立目錄用來存放虛擬環境ip

mkdir ~/.virtualenvs

在.bash_profile中添加

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

生效

source ~/.bashrc

建立virtualenv

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

命令列表

  • workon:列出虛擬環境列表
  • lsvirtualenv:同上
  • mkvirtualenv :新建虛擬環境
  • workon [虛擬環境名稱]:切換虛擬環境
  • rmvirtualenv :刪除虛擬環境
  • deactivate: 離開虛擬環境
相關文章
相關標籤/搜索