virtualenv and virtualenvwrapper on Ubuntu 14.04
In this post I’ll go over my attempt to setup virtual environments for Python development. Most Python users probably don’t want to use virtual environments and should just set up a single user environment that works for their needs. However, if you are working on writing one or more Python modules or packages, these tools are geared towards creating isolated Python environments for each project. This is very useful for keeping track of such things as the minimal Python requirements for each project.
Assuming you want to proceed, my goal is to setup and usevirtualenvwrapper (see virtualenvwrapper docs for more info), a set of shell tools wrapped around the virtualenv package. Both the Doug Hellman post and the simonsoftware post provide some motivation for the development and use of the wrapper formulation– in simple termsvirtualenvwrapper provides some shortcuts for common actions. So, I’ve decided to use the wrapped version.
Basic install
Following the virtualenvwrapper basic install, the installation process is to install virtualenv and virtualenvwrapper using pip. I have already installed virtualenv, as can be seen by using pip show:
$ pip show virtualenv
---
Name: virtualenv
Version: 1.11.6
Location: /home/cstrelioff/.local/lib/python2.7/site-packages
Requires:
If you need to install, the command is – I install as a user:
$ pip install --user virtualenv
Next, install the virtualenvwrapper:
$ pip install --user virtualenvwrapper
Now a pip show should show something like:
$ pip show virtualenvwrapper
---
Name: virtualenvwrapper
Version: 4.3.1
Location: /home/cstrelioff/.local/lib/python2.7/site-packages
Requires: virtualenv, virtualenv-clone, stevedore
Finally, we add some information to our ~/.bashrc file – I added to the end of the file, as usual for such things. The actual contents for you will be different.
- I wanted my virtual environments in ~/virtenvs/ and I had already made that directory.
- I want to keep my active projects in ~/Projects-Active/, an existing directory.
- Finally, because I installed as a user, the path to myvirtualenvwrapper.sh is in ~/.local/bin/, as indicated by usingwhich:
$ which virtualenvwrapper.sh
/home/cstrelioff/.local/bin/virtualenvwrapper.sh
Putting all of that specific information together, I added the following to ~/.bashrc:
# where to store our virtual envs
export WORKON_HOME=$HOME/virtenvs
# where projects will reside
export PROJECT_HOME=$HOME/Projects-Active
# where is the virtualenvwrapper.sh
source $HOME/.local/bin/virtualenvwrapper.sh
After saving the changes, I sourced the file to make the changes active:
$ source ~/.bashrc
Working with virtualenvs
Next up, let’s figure out how to use all this– you should also look at the simonsoftware post for another take on this. The main command to remember is workon, as in I’m going to work on this project. However, if we try it now we get nothing:
$ workon
$
We need to make a virtual environment. So, let’s make one:
$ mkvirtualenv test_env01
New python executable in test_env01/bin/python
Installing setuptools, pip...done.
We can use pip list to see the packages available:
(test_env01)$ pip list
argparse (1.2.1)
pip (1.5.6)
setuptools (3.6)
wsgiref (0.1.2)
Notice that the command prompt has changed to include the environment name. If we want to install a package in this environment we use pip:
(test_env01)$ pip install pyaml
Now, a pip list gives:
(test_env01)$ pip list
argparse (1.2.1)
pip (1.5.6)
pyaml (14.05.7)
PyYAML (3.11)
setuptools (3.6)
wsgiref (0.1.2)
To deactivate the virtual environment, we type exactly what you’d expect:
(test_env01)$ deactivate
$
and we get back to the normal command prompt. However, now theworkon command will show the virtual environment that we created:
$ workon
test_env01
$
To start working on it again, simply try out the following to see everything is there:
$ workon test_env01
(test_env01)$ pip list
argparse (1.2.1)
pip (1.5.6)
pyaml (14.05.7)
PyYAML (3.11)
setuptools (3.6)
wsgiref (0.1.2)
(test_env01)$ deactivate
Projects in virtualenvwrapper
Finally, let’s talk about projects in virtualenvwrapper. This creates both (i) a virtual environment and (ii) a project directory in the location specified by PROJECT_HOME variable in the additions to the~/.bashrc file. Let’s try it out:
$ mkproject test_project02
New python executable in test_project02/bin/python
Installing setuptools, pip...done.
Creating /home/cstrelioff/Projects-Active/test_project02
Setting project for test_project02 to
/home/cstrelioff/Projects-Active/test_project02
(test_project02)~/Projects-Active/test_project02$
Notice that this also creates a directory and cd’s to directory– very nice! Now, if we try pip list we’ll see only the packages for a new environmnet:
(test_project02)~/Projects-Active/test_project02$ pip list
argparse (1.2.1)
pip (1.5.6)
setuptools (3.6)
wsgiref (0.1.2)
Switching between environments
Now we have two virtual environments setup, but only one is setup as a project. We can see both with workon:
$ workon
test_env01
test_project02
To get a sense of how this all works, let startup test_env01 and usepip list to see that PyYAML is installed:
$ workon test_env01
(test_env01)$ pip list
argparse (1.2.1)
pip (1.5.6)
pyaml (14.05.7)
PyYAML (3.11)
setuptools (3.6)
wsgiref (0.1.2)
Next, while in test_env01, let’s switch to test_project02 usingworkon and look at the installed packages (no PyYAML):
(test_env01)$ workon test_project02
(test_project02)~/Projects-Active/test_project02$ pip list
argparse (1.2.1)
pip (1.5.6)
setuptools (3.6)
wsgiref (0.1.2)
Notice that the workon cd’s to the project directory. This happens because we setup test_project02 as a project and not just avirtualenv. If you use workon to switch back to test_env01 there will be no cd because there is no project file associated with that virtual environment. In practice I imagine I will always use mkproject to set things up.
Cleaning up
Finally, to clean up the example above we can use rmvirtualenv:
$ workon
test_env01
test_project02
$ rmvirtualenv test_env01
Removing test_env01...
$ rmvirtualenv test_project02
Removing test_project02...
$ workon
$
With the final workon we can see that all of our environments are gone. However, note that the directory created in PROJECT_HOMEwill not be deleted by the above– this is probably a good default behaviour. You’ll have to go delete the directory (if you want).
That’s it, hopefully some will find this useful post useful. If you have cool/better ways to use these tools leave a comment below.
virtualenv and virtualenvwrapper on Ubuntu 14.04的更多相关文章
- [django] Deploy Django Applications Using uWSGI and Nginx on Ubuntu 14.04
关键点1:chmod-socket=666 (mysite_uwsgi.ini) 关键点2 : 工程目录和虚拟环境目录搞清楚 几个参考: http://uwsgi-docs.readthedocs.i ...
- Ubuntu 14.04 更新 setuptools 至 19.2 版本
参考: Error: "No module named _markerlib" when installing some packages on virtualenv Ubuntu ...
- ubuntu 14.04 部署Django项目
一.购买服务器 推荐 vultr的服务器,还可以_ _ _,链接:传送门 操作系统建议选 ubuntu 14.04 64位 二.购买域名 链接:传送门 三.安装相关软件 # 创建一个叫mu的用户 ro ...
- Ubuntu 14.04中Elasticsearch集群配置
Ubuntu 14.04中Elasticsearch集群配置 前言:本文可用于elasticsearch集群搭建参考.细分为elasticsearch.yml配置和系统配置 达到的目的:各台机器配置成 ...
- deepsooncms在Ubuntu 14.04上部署教程
deepsooncms在Ubuntu 14.04上部署教程 一.安装mono1.在命令行运行sudo apt-key adv --keyserver keyserver.ubuntu.com --re ...
- Ubuntu 14.04 中 安装elasticsearch2.*+logstash2.*+kibana
在Ubuntu 14.04 上安装单机版ELK 2.*(脚本化) 1.判断是否为root权限 if [ "${UID}" -ne 0 ]; then echo "You ...
- 【DDD/CQRS/微服务架构案例】在Ubuntu 14.04.4 LTS中运行WeText项目的服务端
在<WeText项目:一个基于.NET实现的DDD.CQRS与微服务架构的演示案例>文章中,我介绍了自己用Visual Studio 2015(C# 6.0 with .NET Frame ...
- Ubuntu 14.04 LTS下安装Google Chrome浏览器
在Ubuntu 14.04下安装Google Chrome浏览器非常简单,只要到Chrome的网站下载Deb安装包并进行安装即可.当然你也可以使用APT软件包管理器来安装Google Chrome浏览 ...
- 烂泥:ubuntu 14.04搭建OpenVPN服务器
本文由秀依林枫提供友情赞助,首发于烂泥行天下 公司分部需要连接公司内部的服务器,但是该服务器只允许公司内部的网络访问. 为了解决这个问题,打算使用VPN.对于VPN以前使用最多的是PPTP这个解决方案 ...
随机推荐
- IOS内存管理学习笔记
内存管理作为iOS中非常重要的部分,每一个iOS开发者都应该深入了解iOS内存管理,最近在学习iOS中整理出了一些知识点,先从MRC开始说起. 1.当一个对象在创建之后它的引用计数器为1,当调用这个对 ...
- WCF的传输安全(读书笔记)
Wcf的传输安全主要涉及认证.消息的一致性和机密性.Wcf采用两种不同的机制来解决这三个涉及传输安全的问题,即Transport安全模式和Message安全模式. Transport安全模式利用基于传 ...
- Centos用yum升级mysql到(5.5.37)
原文:http://www.if-not-true-then-false.com/2010/install-mysql-on-fedora-centos-red-hat-rhel/ 1. Change ...
- php随机密码函数的实例代码
php随机密码函数的入门例子 时间:2015-12-16 20:42:48来源:网络 导读:php生成随机密码的函数实例,php生成随机密码的函数,生成数字.大小写字母与特殊字符组合的随机密码. ...
- Pattern Lab - 构建先进的原子设计系统
Pattern Lab 是一个工具集,帮助您创建原子设计系统.在它的核心,是一个自定义静态网站生成器,构建了类似原子,分子和界面结合在一起,形成模板和页面.Pattern Lab 可以作为项目的模式库 ...
- Kafka集群部署
一. 关于kafka Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据. 这种动作(网页浏览,搜索和其他用户的行动)是在现代网络上的许多社会功能的一个关键 ...
- [git]fork+pull提交模式
fork+pull提交模式 在公司项目中,大多都是通过"主题分支"的方式,进行开发与合并代码.但是,这样又一个弊端就是:合并代码后需要删除分支.同时,如果是开源的项目的话,非项目中 ...
- [python]逆水行舟不进则退(1)
工作后迎来的第一个长假期,打算在家休息一下,看看书之类的.但是不写点东西,不做点东西,感觉有些浪费时间.同时也想通过做点东西检验下自己这段时间的收获.其实在我开始写这篇文章的时候心里还是很没底的-交代 ...
- 新找到一个安装Android SDk的方法-记录
此方法需使用国内的镜像,但是国内镜像网速不一定要很快. 迅雷下载工具这个是必须的. 今天注意到SDK目录下有一个temp文件夹,打开看了看发现就是缓存的目录,因此想到直接从镜像站下载相应的包来替换,测 ...
- JAVA jdbc(数据库连接池)学习笔记(二) SQL注入
PS:今天偶然间发现了SQL的注入...所以就简单的脑补了一下,都是一些简单的例子...这篇写的不怎么样...由于自己没有进行很深的研究... 学习内容: 1.SQL注入的概念... 所谓SQL注 ...