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
- OK:
How to install a virtualenv.
Assuming you already have Python installed:
- Install Pip.
sudo easy_install pip
- Use Pip to install virtualenv
sudo pip install virtualenv
- Change directories into the folder where you want the virtual environment installed.
cd ~/Desktop/projects/my_app
- Create your virtual env
virtualenv env
- 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.