Virtual Environments for mac
A Virtual Environment is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them. It solves the “Project X depends on version 1.x but, Project Y needs 4.x” dilemma, and keeps your global site-packages directory clean and manageable.
For example, you can work on a project which requires Django 1.3 while also maintaining a project which requires Django 1.0.
virtualenv
virtualenv is a tool to create isolated Python environments. virtualenv creates a folder which contains all the necessary executables to use the packages that a Python project would need.
Install virtualenv via pip:
$ pip install virtualenv
Basic Usage
- Create a virtual environment for a project:
 
$ cd my_project_folder
$ virtualenv venv
virtualenv venv will create a folder in the current directory which will contain the Python executable files, and a copy of the pip library which you can use to install other packages. The name of the virtual environment (in this case, it was venv) can be anything; omitting the name will place the files in the current directory instead.
This creates a copy of Python in whichever directory you ran the command in, placing it in a folder named venv.
You can also use a Python interpreter of your choice.
$ virtualenv -p /usr/bin/python2. venv
This will use the Python interpreter in /usr/bin/python2.7
- To begin using the virtual environment, it needs to be activated:
 
$ source venv/bin/activate
The name of the current virtual environment will now appear on the left of the prompt (e.g. (venv)Your-Computer:your_project UserName$) to let you know that it’s active. From now on, any package that you install using pip will be placed in the venvfolder, isolated from the global Python installation.
Install packages as usual, for example:
$ pip install requests
- If you are done working in the virtual environment for the moment, you can deactivate it:
 
$ deactivate
This puts you back to the system’s default Python interpreter with all its installed libraries.
To delete a virtual environment, just delete its folder. (In this case, it would be rm -rfvenv.)
After a while, though, you might end up with a lot of virtual environments littered across your system, and its possible you’ll forget their names or where they were placed.
Other Notes
Running virtualenv with the option --no-site-packages will not include the packages that are installed globally. This can be useful for keeping the package list clean in case it needs to be accessed later. [This is the default behavior for virtualenv 1.7 and later.]
In order to keep your environment consistent, it’s a good idea to “freeze” the current state of the environment packages. To do this, run
$ pip freeze > requirements.txt
This will create a requirements.txt file, which contains a simple list of all the packages in the current environment, and their respective versions. Later it will be easier for a different developer (or you, if you need to re-create the environment) to install the same packages using the same versions:
$ pip install -r requirements.txt
This can help ensure consistency across installations, across deployments, and across developers.
Lastly, remember to exclude the virtual environment folder from source control by adding it to the ignore list.
virtualenvwrapper
virtualenvwrapper provides a set of commands which makes working with virtual environments much more pleasant. It also places all your virtual environments in one place.
To install (make sure virtualenv is already installed):
$ pip install virtualenvwrapper
$ export WORKON_HOME=~/Envs
$ source /usr/local/bin/virtualenvwrapper.sh
(Full virtualenvwrapper install instructions.)
For Windows, you can use the virtualenvwrapper-win.
To install (make sure virtualenv is already installed):
$ pip install virtualenvwrapper-win
In Windows, the default path for WORKON_HOME is %USERPROFILE%Envs
Basic Usage
- Create a virtual environment:
 
$ mkvirtualenv venv
This creates the venv folder inside ~/Envs.
- Work on a virtual environment:
 
$ workon venv
Alternatively, you can make a project, which creates the virtual environment, and also a project directory inside $PROJECT_HOME, which is cd -ed into when you workonmyproject.
$ mkproject myproject
virtualenvwrapper provides tab-completion on environment names. It really helps when you have a lot of environments and have trouble remembering their names.
workon also deactivates whatever environment you are currently in, so you can quickly switch between environments.
- Deactivating is still the same:
 
$ deactivate
- To delete:
 
$ rmvirtualenv venv
Other useful commands
lsvirtualenv- List all of the environments.
 cdvirtualenv- Navigate into the directory of the currently activated virtual environment, so you can browse its 
site-packages, for example. cdsitepackages- Like the above, but directly into 
site-packagesdirectory. lssitepackages- Shows contents of 
site-packagesdirectory. 
autoenv
When you cd into a directory containing a .env, autoenv automagically activates the environment.
Install it on Mac OS X using brew:
$ brew install autoenv
And on Linux:
$ git clone git://github.com/kennethreitz/autoenv.git ~/.autoenv
$ echo 'source ~/.autoenv/activate.sh' >> ~/.bashrc
Virtual Environments for mac的更多相关文章
- 解决Virtual Box 安装Mac OS X当出现“hfs: summary table not allowed on FS with block size of 2048”问题
		
解决Virtual Box 安装Mac OS X当出现"hfs: summary table not allowed on FS with block size of 2048"问 ...
 - Networked Graphics: Building Networked Games and Virtual Environments (Anthony Steed / Manuel Fradinho Oliveira 著)
		
PART I GROUNDWORK CHAPTER 1 Introduction CHAPTER 2 One on One (101) CHAPTER 3 Overview of the Intern ...
 - 在 Virtual Box 安装 Mac Os 并安装 Qt 开发应用
		
导读 由于 Beslyric-for-X 项目开发需要,开始尝试在 Mac Os 下开发 Qt 应用.尝试成功后,记录于此,希望对有类似需求的人有所帮助. 本文以开发 Beslyric-for-X 为 ...
 - [译]The Python Tutorial#12. Virtual Environments and Packages
		
[译]The Python Tutorial#Virtual Environments and Packages 12.1 Introduction Python应用经常使用不属于标准库的包和模块.应 ...
 - [Python] Manage Dependencies with Python Virtual Environments
		
Virtual Environments ensure that dependencies from one Python application don’t overwrite the depend ...
 - config windows virtual machine on mac
		
1.download virtualbox and related extension pack from http://www.oracle.com/technetwork/server-stor ...
 - Virtual Environments
		
virtualenv 再另一篇随笔中已经提到过virtualenv.如果我们有好几个不同的项目,他们需要的第三方包版本不同,那怎么办呢.这时候就需要virtualenv创建一个虚拟环境,里面包含了一个 ...
 - python Virtual Environments
		
Install $ pip install virtualenv Basic usage 在一个项目中创建一个虚拟环境 $ cd my_project_folder $ virtualenv venv ...
 - docker官方文档学习-1-Docker for mac安装配置
		
https://docs.docker.com/docker-for-mac/ Get started with Docker for Mac 首先像在本博客docker-1-环境安装及例子实践处将环 ...
 
随机推荐
- PHP cURL应用实现模拟登录与采集使用方法详解
			
对于做过数据采集的人来说,cURL一定不会陌生.虽然在PHP中有file_get_contents函数可以获取远程链接的数据,但是它的可控制性太差了,对于各种复杂情况的采集情景,file_get_co ...
 - JAVA字节码解析
			
Java字节码指令 Java 字节码指令及javap 使用说明 ### java字节码指令列表 字节码 助记符 指令含义 0x00 nop 什么都不做 0x01 aconst_null 将null推送 ...
 - QWidget::paintEngine: Should no longer be called
			
Qt新手,其实并不知道出现这个问题的本质原因,我的问题在于paintEvent中使用的painter是类的成员而不是临时新建的局部变量,改为使用局部变量问题就消失了.
 - [转]javascript的urlencode
			
今天在一个原来使用AJAX自动缩小选择内容的项目上突然发现当输入名称时,如果输入有特殊字符&的时候,选择的内容不会发生变化,也就是说输入的内容在&后面的内容会被截断,经过查证才发现在客 ...
 - ruby include和exclude区别
			
很久没玩ruby了,今天看源码的时候,看到extend硬是缓不过神了,Google下extend和include的区别,做个记录 在class中include module, 那么module中的方法 ...
 - MyBatis知多少(22)MyBatis删除操作
			
本节从表中使用MyBatis删除记录. 我们已经在MySQL下有EMPLOYEE表: CREATE TABLE EMPLOYEE ( id INT NOT NULL auto_increment, f ...
 - Simplified CommonJS wrapper 与 AMD 标准定义模块差异
			
标准的 AMD 模块定义 一个标准的 AMD 模块看起来是这样子: define(['foo', 'foo/bar'], function (foo, bar) { return { doSometh ...
 - table相关的API
			
void lua_getglobal (lua_State *L, const char *name);获取lua全局变量,将lua的全局变量global name压栈.堆栈+1 void lua_s ...
 - 疯狂的ASP.NET系列-第一篇:啥是ASP.NET
			
最近想学下ASP.NET,于是在网店上看到一本书叫做ASP.NET高级程序设计,老婆在旁边问了句:“这个不是DSP(数字信号处理,大学读的电子,所以这个比较熟),是ASP啊,什么是ASP啊?”.我想了 ...
 - AC_Dream 1224 Robbers(贪心)
			
题意:n个抢劫犯分别抢到的金钱是k1, k2, k3,...,一共得到的金钱是m, 但是在分钱的时候是按照x1/y, x2/y, x3/y,....的比例进行分配的!这样的话 一些抢劫犯就会觉得不公平 ...