PythonVirtualEnv: Difference between revisions

From UMIACS
Jump to navigation Jump to search
No edit summary
No edit summary
Line 3: Line 3:
virtualenv can be found in the following locations:
virtualenv can be found in the following locations:
*'''RHEL5'''
*'''RHEL5'''
:*<code>usr/local/stow/virtualenv-1.5.1</code>
:<code>usr/local/stow/virtualenv-1.5.1</code>
*'''RHEL6'''
*'''RHEL6'''
:*<code>/opt/local/stow/virtualenv-1.9.1</code>
:<code>/opt/local/stow/virtualenv-1.9.1</code>


==Basic Usage==
==Basic Usage==

Revision as of 22:03, 13 May 2014

A virtual environment is an isolated working copy of Python which allows you to to work on specific projects, without affecting others. It creates an environment that has its own installation directories, that doesn’t share libraries with other virtualenv environments (and optionally doesn’t access the globally installed libraries either).

virtualenv can be found in the following locations:

  • RHEL5
usr/local/stow/virtualenv-1.5.1
  • RHEL6
/opt/local/stow/virtualenv-1.9.1

Basic Usage

The following steps outline how to create a virtual environment using the system Python. Please note the that following examples were done using RHEL5. Please adjust the commands to reflect the OS you are on.

Creating the virtual environment

-bash-3.2$ which python
/usr/bin/python

-bash-3.2$ /usr/local/stow/virtualenv-1.5.1/virtualenv.py env
New python executable in env/bin/python
Installing setuptools.............done.

-bash-3.2$ source env/bin/activate

(env)-bash-3.2$ which python
~/env/bin/python

You will notice that once you have created your virtual environment, you will need to use the 'source' command to load it into your environment.

Installing Python Modules

Once you have created your virtual environment and sourced it, you can install additional modules using the 'pip' command.

(env)-bash-3.2$ pip install nose
Downloading/unpacking nose
  Downloading nose-1.3.3.tar.gz (274Kb): 274Kb downloaded
  Running setup.py egg_info for package nose

...output omitted for brevity...

Successfully installed nose
Cleaning up...
(env)-bash-3.2$ 

Listing installed Python Modules

(env)-bash-3.2$ pip freeze
nose==1.3.3

Uninstalling Python Modules

'pip' can also be used to remove a module from the environment.

(env)-bash-3.2$ pip uninstall nose
Uninstalling nose:
  /chimerahomes/sabobbin/env/bin/nosetests
  /chimerahomes/sabobbin/env/bin/nosetests-2.4
  /chimerahomes/sabobbin/env/lib/python2.4/site-packages/nose
  /chimerahomes/sabobbin/env/lib/python2.4/site-packages/nose-1.3.3-py2.4.egg-info
  /chimerahomes/sabobbin/env/man/man1/nosetests.1
Proceed (y/n)? y
  Successfully uninstalled nose

Switching between virtual environments

To switch between different environments, simply deactivate your current virtual environment, and source the other.

(env)-bash-3.2$ which python
~/env/bin/python

(env)-bash-3.2$ deactivate 
-bash-3.2$ source env2.7/bin/activate

(env2.7)-bash-3.2$ which python
~/env2.7/bin/python

Using a different python version

To create a virtual environment that uses a version of python that is different then the system default, simply created the virtual env with your target version of Python.

-bash-3.2$ module load Python

-bash-3.2$ which python
/usr/local/stow/python-2.7.2/bin/python

Important: virtualenv will include any modules listed in your PYTHONPATH when initializing the virtual environment. To ensure a vanilla environment, it might be a good idea to verify your PYTHONPATH is empty.

-bash-3.2$ echo $PYTHONPATH
/usr/local/stow/python-commonmodules-2.7.2.0/lib/python2.7/site-packages

-bash-3.2$ PYTHONPATH=''
-bash-3.2$ /usr/local/stow/virtualenv-1.5.1/virtualenv.py env2.7
New python executable in env2.7/bin/python
Installing setuptools....................done.

-bash-3.2$ source env2.7/bin/activate
(env2.7)-bash-3.2$ which python
~/env2.7/bin/python