您的位置:首页 > 编程语言 > Python开发

virtualenv -- python虚拟沙盒

2015-11-13 08:51 459 查看
有人说:virtualenv、fabric和pip是pythoneer的三大神器。

不管认不认同,至少要先认识一下,pip现在倒是经常用到,virtualenv第一次听说,不过,总得尝试一下吧。

一、安装

$sudopipinstallvirtualenv

因为我已经安装了pip,那么就直接用pip来安装了,简单方便。

其它的安装方式请参考官方网站:http://www.virtualenv.org/en/latest/index.html

二、创建虚拟环境



root@kali:/recall/code#virtualenvtest_envNewpythonexecutableintest_env/bin/python
Installingsetuptools,pip...done.root@kali:/recall/code#

很简单,就是virtualenv环境名称[自定义的名称,自己喜欢什么就写什么]

默认情况下,虚拟环境会依赖系统环境中的sitepackages,就是说系统中已经安装好的第三方package也会安装在虚拟环境中,

如果不想依赖这些package,那么可以加上参数

--no-site-packages 

建立虚拟环境

即变成了:


root@kali:/recall/code#virtualenvtest_env--no-site-packages
Newpythonexecutableintest_env/bin/python
Installingsetuptools,pip...done.root@kali:/recall/code#

三、启动虚拟环境

创建成功后,会在当前目录下生成对应的目录文件。


root@kali:/recall/code#ls-ltest_env/总用量16drwxr-xr-x2rootroot40964月2920:03bindrwxr-xr-x2rootroot40964月2919:58includedrwxr-xr-x3rootroot40964月2919:58libdrwxr-xr-x2rootroot40964月2919:58localroot@kali:/recall/code#


我们先进入到该目录下:

cdtest_env/

然后启动

root@kali:/recall/code/test_env#source./bin/activate

启动成功后,会在前面多出test_env字样,如下所示

(test_env)root@kali:/recall/code/test_env#

四、使用测试

(test_env)root@kali:/recall/code/test_env#pipinstallrequestsDownloading/unpackingrequests
Downloadingrequests-2.2.1-py2.py3-none-any.whl(625kB):625kBdownloaded
Installingcollectedpackages:requests
Successfullyinstalledrequests
Cleaningup...
(test_env)root@kali:/recall/code/test_env#pythonPython2.7.3(default,Jan22013,13:56:14)
[GCC4.7.2]onlinux2
Type"help","copyright","credits"or"license"formoreinformation.>>>importrequests>>>
>>>response=requests.get("http://www.baidu.com")>>>response.status_code200
>>>

五、退出虚拟环境

deactivate

完整如下:

(test_env)root@kali:/recall/code/test_env#pythonPython2.7.3(default,Jan22013,13:56:14)
[GCC4.7.2]onlinux2
Type"help","copyright","credits"or"license"formoreinformation.>>>importrequests>>>
>>>response=requests.get("http://www.baidu.com")>>>response.status_code200
>>>exit()
(test_env)root@kali:/recall/code/test_env#deactivateroot@kali:/recall/code/test_env#


更多资料请百度、google或查看官方文档。

文章转载:/article/5043869.html

下篇结合VirtualEnvWrapper管理VirtualEnv
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: