virtualenv -- python虚拟沙盒

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

一、安装

pip install virtualenv

二、创建虚拟环境

root@kali:/recall/code# virtualenv test_env
New python executable in test_env/bin/python
Installing setuptools, pip...done.
root@kali:/recall/code#

三、启动虚拟环境

    root@kali:/recall/code# ls -l test_env/
总用量 16
drwxr-xr-x 2 root root 4096 4月 29 20:03 bin
drwxr-xr-x 2 root root 4096 4月 29 19:58 include
drwxr-xr-x 3 root root 4096 4月 29 19:58 lib
drwxr-xr-x 2 root root 4096 4月 29 19:58 local
root@kali:/recall/code#
  1. 我们先进入到该目录下:

    cd test_env/
  2. 然后启动
root@kali:/recall/code/test_env# source ./bin/activate

Windows下启动命令:

virtualenv envname  ##创建一个新的隔离环境
cd envname
Scripts\activate ##激活并切换到virtualenv环境
  1. 启动成功后,会在前面多出 test_env 字样,如下所示
(test_env)root@kali:/recall/code/test_env#

四、使用测试

(test_env)root@kali:/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)root@kali:/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" for more information.
>>> import requests
>>>
>>> response = requests.get("http://www.baidu.com")
>>> response.status_code
200
>>>

五、退出虚拟环境

deactivate

virtualenvwrapper

  1. 安装
# windows
pip install virtualenvwrapper-win # Linux
pip install virtualenvwrapper
  1. 设置WORKON_HOME环境变量

virtualenvwrapper默认将所有的虚拟环境放在~/.virtualenvs目录下管理,可以修改环境变量WORKON_HOME来指定虚拟环境 的保存目录。

WORKON_HOME=D:\ven_py

# linux (放到/etc/profile 或 ~/.bash_profile)
export WORKON_HOME=$HOME/.virtualenvs # 自定义
export PROJECT_HOME=$HOME/workspace
source /usr/local/bin/virtualenvwrapper.sh
  1. 新建虚拟环境
mkvirtualenv venv_py27
  1. 查看安装的所有虚拟环境

    workon

  2. 进入虚拟环境

    workon venv_py27

  3. 退出虚拟环境

    deactivate

1.windows下搭建virtualenv、virtualenvwrapper虚拟环境

2.python------virtualenv&virtualenvwrapper的使用

Linux下安装多版本Python时用virtualenvwrapper.sh

  1. 安装了python3后用pip3安装了virtualenvwrapper
  2. 在用workon创建虚拟环境时报以下问题
/usr/bin/python: No module named virtualenvwrapper
/usr/bin/python: No module named virtualenvwrapper
解决方法:
  1. 查看virtualenvwrapper.sh的位置
➜  ~ whereis virtualenvwrapper.sh
virtualenvwrapper: /usr/local/bin/virtualenvwrapper.sh
  1. 查看virtualenvwrapper.sh, 会看到有个VIRTUALENVWRAPPER_PYTHON变量, 默认是$(command \which python) : 也就是/usr/bin/python 默认情况下就是python27
➜  ~ vim /usr/local/bin/virtualenvwrapper.sh
...
47 # Locate the global Python where virtualenvwrapper is installed.
48 if [ "${VIRTUALENVWRAPPER_PYTHON:-}" = "" ]
49 then
50 VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
51 fi
  1. 根据以上可以直接设置环境变量VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.6
exprot VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.6

还可以将默认的python改为python3 或 直接用默认的python版本安装virtualenvwrapper

python虚拟环境 -- virtualenv , virtualenvwrapper的更多相关文章

  1. Python 虚拟环境[virtualenv/virtualenvwrapper]设置

      virtualenv virtualenv 是一个可以在同一计算机中隔离多个python版本的工具.有时,两个不同的项目可能需要不同版本的python,如 python2.6.6 / python ...

  2. python虚拟环境--virtualenv和virtualenvwrapper

    python虚拟环境--virtualenv和virtualenvwrapper http://www.cnblogs.com/technologylife/p/6635631.html https: ...

  3. Python 虚拟环境Virtualenv

    本人也是Python爱好者,众所周知,Python扩展多,每次为了测试,安装各种各样的扩展,这样导致本地的Python环境非常混乱,就有人想到搞个隔离环境  和 本地环境没有关系,随时可以删除这个隔离 ...

  4. 使用虚拟环境virtualenv/Virtualenvwrapper隔离多个python

    virtualenv 系统中的多个python混用会导致$PYTHONPATH混乱,或者各个工程对于package的版本要求不同等等情况.有一个简单的解决方案就是用virtualenv来隔离多个pyt ...

  5. Centos7 python虚拟环境virtualenv和virtualenvwrapper简单介绍

    我的系统版本是 [root@localhost ~]# cat /etc/os-release 我的Python版本是 [root@localhost ~]# python3 -V 关于如何安装Pyt ...

  6. windows下python虚拟环境virtualenv安装和使用

    前面介绍了python在ubuntu16.04环境下,python的虚拟环境virtualenv的安装,下面介绍在windows环境下的安装和使用. 环境信息 操作系统:windows10 pytho ...

  7. python虚拟环境virtualenv简介

    参考网站: https://realpython.com/python-virtual-environments-a-primer/ 一. 创建一个新的虚拟环境 # Python 2: $ virtu ...

  8. [转载]windows下安装Python虚拟环境virtualenv,virtualenvwrapper-win

    1 前言 由于Python的版本众多,还有Python2和Python3的争论,因此有些软件包或第三方库就容易出现版本不兼容的问题. 通过 virtualenv 这个工具,就可以构建一系列 虚拟的Py ...

  9. #Python学习#python虚拟环境——virtualenv

    前言 在Ubuntu系统中,系统一般会默认安装python2.x和3.x,像我近期买的阿里云ECS默认安装了2.7.2和3.5.2,所有pip安装的第三方包都会被放在默认的site-apckages目 ...

随机推荐

  1. JAR 介绍-百度百科

    JAR(Java Archive,Java 归档文件)是与平台无关的文件格式,它允许将许多文件组合成一个压缩文件.为 J2EE 应用程序创建的 JAR 文件是 EAR 文件(企业 JAR 文件). J ...

  2. 通过HttpServletRequest获取服务器路径

    String scheme = request.getScheme(); String serverName = request.getServerName(); int port = request ...

  3. springmvc json 406

    spring 4.0 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="ht ...

  4. Newbe.Claptrap - 一套以 “事件溯源” 和“Actor 模式”作为基本理论的服务端开发框架

    本文是关于 Newbe.Claptrap 项目主体内容的介绍,读者可以通过这篇文章,大体了解项目内容. 轮子源于需求 随着互联网应用的蓬勃发展,相关的技术理论和实现手段也在被不断创造出来.诸如 “云原 ...

  5. springboot 单体架构之shiro集成

    这里使用的是eclipse 开发工具 1.springboot 版本是2.0的,引入了2个shiro 的依赖,如下 <parent> <groupId>org.springfr ...

  6. Windows服务设置

    [服务列表]services.msc [注册服务] 描述:在注册表和服务数据库中创建服务项. 用法:sc <server> create [service name] [binPath= ...

  7. 并发编程之 Thread 类过期方法和常用方法

    前言 在 Java 刚诞生时,Thread 类就已经有了很多方法,但这些方法由于一些原因(有一些明显的bug或者设计不合理)有些已经废弃了,但是他们的方法名却是非常的好,真的是浪费.我们在进行并发必编 ...

  8. C++ 小知识点 WINAPI

    int WINAPI WINMain 中,WINAPI含义 网友给出回答:在windef.h头文件中有如下定义#define WINAPI      __stdcall#define APIENTRY ...

  9. EWS 流通知订阅邮件

    摘要 查找一些关于流通知订阅邮件的资料,这里整理一下. 核心代码块 using System; using System.Collections.Generic; using System.Linq; ...

  10. thinkphp 网址后台典型页面

    知识点: 1.select a提交后 返回选中项 选中项 value是id 但是要显示name b遍历和列举两种形式 <select name="class_id" clas ...