The Python Virtual Environments system is a tool to make sure that packages related to different projects are stored in different places so that they do not conflict. A Python Virtual Environment is isolated from your other python projects or environments and allows for that environment to have its own dependencies to remain independent.

WINDOWS

Getting Started

  1. Install Python
    1. If you are on an ENGINEERING Windows Desktop, install Python 2.7 through software center
       
  2. Add Python to PATH
    1. Navigate: Control Panel -> User Accounts -> Change my Environment Variables
    2. Inside the top window labeled 'User Variables' select 'Path' and edit it
    3. Select 'Browse' and navigate to C:\python27 and select 'ok'
    4. Select 'Move Down' and repeat step 3 for C:\python27\Scripts
       
  3. Open a new CMD prompt (Windows Key + R, cmd.exe)
     
  4. Install virtualenv through the command pip install virtualenv. If you are not an administrator do not update if prompted
     
  5. Check that virtualenv is installed through the command pip --version
     
  6. Install virtualenvwrapper-win through the command pip install virtualenvwrapper-win

 

Test Functionality

  • Create a test environment with mkvirtualenv example, and this will make a folder called Envs in your user directory
  • Change your python directories to a project folder in command prompt and run setprojectdir to set that as your development folder.
  • To leave an environment use deactivate
  • You can enter an environment by using workon example and from there can invoke package installations like pip install flask
  • To completely delete an environment make sure you leave the environment first and use rmvirtualenv example to delete the environment

     

LINUX

Getting Started

Virtual Environments on Python 2.7

  •  Create a virtual environment in your current directory for a project with the command: virtualenv my_project
    • "my_project" is whatever name you would like to give this environment
  • To create a virtual environment with a specific version of python use the command: virtualenv -p /usr/bin/python2.7 my_project
    • "python2.7" can be replaced with the version you would like to use

Virtual Environments on Python 3.5+

  • Create a virtual environment in your current directory for a project with the command: python3 -m venv my_project
    • "my_project" is whatever name you would like to give this environment

Using the Virtual Environment

Bash shell:

  • Start the virtual environment by activating with the command: source my_project/bin/activate

csh /tcsh:

  • Start the virtual environment by activating with the command: source my_project/bin/activate.csh

Pip can now be used as usual for package installs

  • When you run 'python' it will be the version specified in Step 1
  • When you run 'pip', you're running the correct version
  • All modules are installed inside that environment

When you are finished working in the environment, exit using the command: deactivate

For more details visit:  http://docs.python-guide.org/en/latest/dev/virtualenvs/ 

or download the PDF:  python-guide.pdf