Setting up virtualenv using virtualenvwrapper

I would like to explain how and why we use virtualenv and virtualenvwrapper when we develop apps using Django.

Why?

We usually work on multiple projects in a single system. Each project might(mostly will) have different versions of dependencies. Lets say, I work on Django 1.5 for one project and Django 1.7 for another. So, it is always advisable to isolate the environments. This way, we can install all the dependencies of a project in one environment and easily switch between different environments when switching different projects.

How?

We can activate virtual environments in two ways:

    1. virtualenv 
       source /path/to/envname/bin/activate

    2. Virtualenvwrapper(easy use of virtualenv) - I prefer this way.

Virtualenvwrapper:

Why ?

  1. Easier
  2. All virtual environments at one place
  3. Hooks.

How:

Install virtualenvwrapper globally

apt-get install virtualenvwrapper (use sudo if not root)

Configure virtualenvwrapper:

Add below line to .bashrc

export WORKON_HOME=~/.virtualenvs (or some directory name)

Create virtual environment

mkvirtualenv envname

Use virtual env

workon envname

Deactivate currently active environment

deactivate

Delete virtual environment. This deletes the given environment. You have to deactivate first before deleting.

rmvirtualenv envname

Hooks

Open /path/to/envname/bin/postactivate (There are other hooks too like preactivate).

For example, we can use postactivate hook to perform some action once the environment is activated.

I use this to go to the project directory after activing the environment.

I would add

cd /path/to/project

in postactivate hook so after running below command:

workon  envname

then the current working directory will be changed to the /path/to/project (as provided in the /path/to/envname/bin/postactivate hook)

Now, since you got the environment ready, you can start installing Django(lets say) after activating the corresponding environment.

pip install Django

This will install the latest stable version of Django from pypi.

If you want to install particular version, you need to use double equals(==):

pip install Django==1.6.7

Recent Posts

Archive

2022
2021
2020
2019
2018
2017
2016
2015
2014

Tags

Authors

Feeds

RSS / Atom