python 安装virtualenv和virtualenvwrapper的方法
1.首先介绍pip常用命令
pip安装命令:pipinstallpackage_name
pip升级命令:pipinstall–ungragepackage_name
pip卸载命令:pipuninstallpackage_name
如
pipinstalldjango
pipinstall-Udjango
2.virtualenv的安装
virtualenv的安装:
$sudopipinstallvirtualenv
或
$sudoapt-getinstallpython-virtualenv
如果是MacOSX系统,就可以用easy_install安装virtualenv:
$sudoeasy_installvirtualenv
查看virtualenv的版本号,或检查系统是否是否安装了virtualenv:
$virtualenv--version
用virtualenv创建虚拟环境,一般虚拟环境被命名为venv:
$virtualenvvenv
激活这个虚拟环境:
$sourcevenv/bin/activate
如果使用微软windows系统,激活命令是:
$venv\Script\activate
激活虚拟环境的命令会修改命令行提示符,加入环境名:
(venv)$
当虚拟环境中的工作完成后,如果你想回到全局Python解释器中,可以在命令行提示符下输入deactivate
执行以下命令可在虚拟环境中安装Flask
(venv)$pipinstallflask
验证Flask是否正确安装:
(venv)$python
>>>importflask
>>>
3.virtualenvwrapper的安装
virtualenvwrapper的安装:
$sudopipinstallvirtualenvwrapper
安装完成后,会在下面的位置生成virtualwrapper的shell脚本。
/usr/local/bin/virtualenvwrapper.sh
在使用virtualenvwrapper时,需要配置登录的shell初始化脚本,将virtualenvwrapper.sh的信息读入当前的shell环境。这里以base为例,通过对用户根目录下(即/home/[username])的.bashrc配置文件进入如下修改即可。
修改.bashrc:
if[-f/usr/local/bin/virtualenvwrapper.sh];then
exportWORKON_HOME=$HOME/.virtualenvs
source/usr/local/bin/virtualenvwrapper.sh
fi
再次读入.bashrc:
$source~/.bashrc
我们可以输入命令mkvirtualenv,看一下其是否可用。
$mkvirtualenv--help
设置好之后,你就可以通过下面的命令来操作虚拟环境了:
建立虚拟环境:
$mkvirtualenvenv
确认虚拟环境:
$ls-la$HOME/.virtualenvs
与virtualenv的相同处与不同处,其中,退出虚拟运行环境的命令同样是deactivate,进入虚拟运行环境的命令变为workon。
退出虚拟环境:
(venv)$deactivate
进入已存在的环境或者切换环境,假设存在虚拟环境名为env:
$workonenv
浏览虚拟环境:
$workon
删除虚拟环境:
$rmvirtualenvenv
4.在虚拟环境下一键安装pip常用package
requirements.txt文件里写入package或package==版本号或package>=版本号:
Django==1.7.7
django-debug-toolbar
ply
MySQL-python
uwsgi
flup
Flask
Pillow
markdown2
一键安装命令:
(venv)$pipinstall-rrequirements.txt
在执行上面的一键安装命令过程中,配置MySQL-Python的时候系统报错,提示:
EnvironmentError:mysql_confignotfound
google搜索EnvironmentError:mysql_confignotfound,在stackoverflow找到答案
(venv)$sudoapt-getinstalllibmysqlclient-dev
OK,Enjoyit!!!
以上这篇python安装virtualenv和virtualenvwrapper的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。