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.

时间: 2024-10-17 03:53:09

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.io/en/latest/tutorials/Django_and_nginx.html https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-uwsgi-and-nginx-on-

Ubuntu 14.04 LTS 源码安装Juno版Keystone开发环境

系统环境: Ubuntu 14.04 LTS Python 2.7 一.系统上已经安装如下工具: git     setuptools     pip     msgfmt     virtualenv 二.获取源码 $ git clone https://github.com/openstack/keystone $ cd keystone 三.安装一些pip不支持的依赖 $ sudo apt-get install python-dev libxml2-dev libxslt1-dev li

Linux内核编译 Ubuntu 14.04.3 server 升级至3.19.8

读书笔记:<Linux内核设计与实现>,原书第3版,陈莉君 康华 译 第2章:从内核出发     2.3节:编译内核 实验: ============================================================ 系统环境:VM虚拟机 Ubuntu 14.04.3 LTS server版 任务:编译安装新的内核 注意:不要跨大版本,我在3.19版本内 耗时:2小时 所有版本的内核: https://www.kernel.org/pub/linux/kernel

Linux -&gt;&gt; UBuntu 14.04 LTE下安装Hadoop 1.2.1(集群分布式模式)

安装步骤: 1) JDK -- Hadoop是用Java写的,不安装Java虚拟机怎么运行Hadoop的程序: 2)创建专门用于运行和执行hadoop任务(比如map和reduce任务)的linux用户,就像windows下的服务账户,并且授权给他可以访问运行JDK目录权限,让他可以执行java虚拟机.这个账户最后用来运行bin\.start_all.sh启动hadoop的所有服务,那运行账户肯定是他了,他肯定要有足够的权限.再者,需要配置这个账户的个人环境变量,把Java虚拟机的主目录地址环境

Install postfix on Ubuntu 14.04.txt

Ubuntu 14.04上安装和配置Postfix邮件服务详细教程 Postfix: 用来接受和发送邮件的邮件服务器,正确说法应该叫邮件传送代理(Mail Transfer Agent,MTA),是邮件服务最重要的部分:    Dovecot: POP 和 IMAP 服务器,用来管理本地邮件目录以便用户能通过 Mail.app, Thunderbird, Mutt 等邮件客户端(又叫邮件用户代理 Mail User Agent, MUA)登陆和下载邮件:    Postgrey: 邮件灰名单工具

ubuntu 14.04 install fcitx

ubuntu 14.04 input method sudo apt-get install fcitx-googlepinyin sudo apt-get install fcitx-sunpinyin language support 里边设置keyboard input method system: fcitx input method 选择fcitx restar the x-window 顶部状态栏会出现键盘图标点击选择config current input method 弹出串口点

ubuntu 14.04编译安装xen4.4总结

1. 安装环境 操作系统:ubuntu14.04 xen版本:xen4.4 2. 依赖包的安装 在安装xen之前先进行依赖包的安装,在不停得尝试之后,总结出以下需要安装的依赖包. suso apt-get install gcc make binutils zlib1g-dev python-dev sudo apt-get install libncurses5-dev libcurl4-openssl-dev libx11-dev sudo apt-get install uuid-dev

NVIDIA DIGITS 学习笔记(NVIDIA DIGITS-2.0 + Ubuntu 14.04 + CUDA 7.0 + cuDNN 7.0 + Caffe 0.13.0)

转自:http://blog.csdn.net/enjoyyl/article/details/47397505?from=timeline&isappinstalled=0#10006-weixin-1-52626-6b3bffd01fdde4900130bc5a2751b6d1 NVIDIA DIGITS-2.0 + Ubuntu 14.04 + CUDA 7.0 + cuDNN 7.0 + Caffe 0.13.0环境配置 引言 DIGITS简介 DIGITS特性 资源信息 说明 DIGI

Ubuntu 14.04(x64) 安装cuda8.0

由于之前已经在Ubuntu 14.04 x64上面安装cuda7.0+caffe, 并且已经配置好,caffe也已经跑通. 但是最近需要使用Torch,而Torch对cuda的要求是8.0,因此决定对cuda的版本进行升级,以满足Torch平台的需求. 而最新版的caffe也已经支持cuda8.0. 话不多说,开始安装cuda8.0. 显卡:GeForce GTX TITAN X 系统:Ubuntu 14.04(x_64) CUDA:cuda_8.0.61_375.26_linux.run cu