Tuesday, 10 September 2013

Python virtualenv on windows 7 quick tutorial

Install virtualenv via pip or easy_install

pip install virtualenv

Make sure site_packages (C:\Python27\Lib\site-packages) is in your env path

See here if you dont know how to set an environment variable in windows

http://objectisnull.blogspot.co.uk/2013/10/set-python-environment-variable-windows.html

Best practice is to make a directory where all your virtual environments will live. So, maybe in your home dir,

mkdir virt_env

The default python interpreter that will be used is the one that you used to install virtualenv.

To create a new virtual env, enter:

virtualenv name_of_dir

So typing "virtualenv virt1" (ignore the quotes) will create a new directory called virt1, and it will have your default python instance, easy_install, pip, its own package directory and everything else you need.

A couple of useful arguments that you can pass when creating your virtual env are:

--no-site-packages (this means give access to the global site packages in your virtual env. I think this is the right way to do it, otherwise things can get messy)

--python=path_to_a_python_interpreter (this is how you create a virtual env for a specific python interpreter, probably the most useful thing you can do with virtualenv)

Now, to use a virtual env that you have created. Just execute activate inside the virtualenv's scripts directory:

virt_env\virt2\scripts\activate

You will now see your command window change to show that you are using that virtual environment. Try using pip install some_package, and you will see that the newly downloaded package is installed in your virtualenv, and not in the main python installations site_packages/

Enjoy

No comments:

Post a Comment