linux python 沙盒,virtualenv -- python虚拟沙盒(linux版本)(示例代码)

有人说:virtualenv、fabric 和 pip 是 pythoneer 的三大神器。

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

一、安装

pip install virtualenv

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

二、创建虚拟环境

[email protected]:/recall/code# virtualenv test_env

New python executable in test_env/bin/python

Installing setuptools, pip...done.

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

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

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

建立虚拟环境

即变成了:

[email protected]:/recall/code# virtualenv test_env --no-site-packages

New python executable in test_env/bin/python

Installing setuptools, pip...done.

三、启动虚拟环境

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

48304ba5e6f9fe08f3fa1abda7d326ab.png

[email protected]:/recall/code# ls -l test_env/

总用量 16drwxr-xr-x 2 root root 4096 4月 29 20:03bin

drwxr-xr-x 2 root root 4096 4月 29 19:58include

drwxr-xr-x 3 root root 4096 4月 29 19:58lib

drwxr-xr-x 2 root root 4096 4月 29 19:58local

48304ba5e6f9fe08f3fa1abda7d326ab.png

我们先进入到该目录下:

cd test_env/

然后启动

[email protected]:/recall/code/test_env# source ./bin/activate

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

(test_env)[email protected]:/recall/code/test_env#

四、使用测试

48304ba5e6f9fe08f3fa1abda7d326ab.png

(test_env)[email protected]:/recall/code/test_env# pip install requests

Downloading/unpacking requests

Downloading requests-2.2.1-py2.py3-none-any.whl (625kB): 625kB downloaded

Installing collected packages: requests

Successfully installed requests

Cleaning up...

(test_env)[email protected]:/recall/code/test_env# python

Python 2.7.3 (default, Jan 2 2013, 13:56:14)

[GCC 4.7.2] on linux2

Type "help", "copyright", "credits" or "license" formore information.

>>> importrequests

>>>

>>> response = requests.get("http://www.baidu.com")

>>>response.status_code

200

>>>

48304ba5e6f9fe08f3fa1abda7d326ab.png

五、退出虚拟环境

deactivate

完整如下:

48304ba5e6f9fe08f3fa1abda7d326ab.png

(test_env)[email protected]:/recall/code/test_env# python

Python 2.7.3 (default, Jan 2 2013, 13:56:14)

[GCC 4.7.2] on linux2

Type "help", "copyright", "credits" or "license" formore information.

>>> importrequests

>>>

>>> response = requests.get("http://www.baidu.com")

>>>response.status_code

200

>>>exit()

(test_env)[email protected]:/recall/code/test_env# deactivate

[email protected]:/recall/code/test_env#

48304ba5e6f9fe08f3fa1abda7d326ab.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值