Headache-Free Installation: How to set up a virtual environment

One of the most irritating things about learning a new framework or switching to a new computer is often not learning the different nuances in syntax or even debugging — is installing the darn thing. I can recount the numerous dependency and version management errors I was met with upon when forking my friend’s Django project or deciding to pick up Flask, both Python web frameworks.

When I discovered virtual environments, a lot of these headaches disappeared. Virtual environments allow you to compartmentalize your framework and package installations into the clean slate, separate from the rest of your computer.

Why a virtual environment?

When people type ‘sudo’ before attempting to install something, I fret at the amount of trouble they may be causing themselves later down the line. Installing something globally will inevitably lead to version management problems and other clashes down the line.

What to install globally

  • Python (usually comes installed)
    • Easy_install (comes with Python)
  • Pip (Python package manager)

What to NOT install globally

  • Packages
    • OK: sudo easy_install pip
    • May lead to problems: sudo pip install django

How to install a virtualenv.

Assuming you already have Python installed:

  1. Install Pip.
    sudo easy_install pip
  2. Use Pip to install virtualenv
    sudo pip install virtualenv
  3. Change directories into the folder where you want the virtual environment installed.
    cd ~/Desktop/projects/my_app
  4. Create your virtual env
    virtualenv env
  5. Activate your virtual environment
    source env/bin/activate

Now your command line will include the virtual environment:

And you can install frameworks like django and packages in this new environment as necessary.

Notes

  • Your environment will only exist when you activate it. Notice that if you open a new terminal window, it disappears.
  • You can deactivate the environment by typing
    exit
    Or closing the terminal window.

Leave a Reply

Your email address will not be published. Required fields are marked *