Introduction to virtualenv

If you’re going to the gym, you’re going to dress one way. If you’re going to work, you’re probably going to dress another way. Python is the same! Not with clothes, though, but with packages.

Let’s say there’s a Python package for web scraping called BeautifulSoup (which there is!). Maybe in one project you need BeautifulSoup 3 and in one project you need BeautifulSoup 4 - in this step we’re now going to install a tool that helps keep them separate. It’s useful, too - you’ll use BeautifulSoup in just a few weeks.

These separate areas for separate packages (or outfits!) are called virtual environments. To control them we need to install a package called virtualenv.

Step 1: Installing virtualenv

Since virtualenv is a package, we’ll use pip to install it (“Pip Installs Packages,” remember?). Because we’re using Python 3, we use pip3 - unless we’re inside of a virtual environment, pip is for Python 2.

pip3 install virtualenv

Read your results! If you ever get a Permission denied notice when using pip, type sudo pip instead - in this case, your command would be sudo pip3 install virtualenv

Now you’ll set virtualenv to always look for cool new Python 3 instead of terrible old Python 2.

echo 'export VIRTUALENV_PYTHON=`which python3`' >> ~/.bash_profile
source ~/.bash_profile

This adds a line to our ~/.bash_profile without having to open it up. If you’d like to look at it, you can cat ~/.bash_profile or open it up

Step 2: Installing virtualenvwrapper

There are also a few add-ons to virtualenv that make virtualenv easier to use. Let’s install one of them called virtualenvwrapper. It will give us a few friendlier commands.

virtualenvwrapper is a package, so we’ll use pip3 to install it.

pip3 install virtualenvwrapper

Once it’s done installing (remember the sudo note up above!), we can configure it.

echo 'export VIRTUALENVWRAPPER_PYTHON=$VIRTUALENV_PYTHON' >> ~/.bash_profile
echo 'export WORKON_HOME=$HOME/.virtualenvs' >> ~/.bash_profile
export VIRTUALENVWRAPPER_SH_PATH=`which virtualenvwrapper.sh`
echo "source $VIRTUALENVWRAPPER_SH_PATH" >> ~/.bash_profile
source ~/.bash_profile

These lines make sure we’re using the right Python, and tells virtualenv where to store all of our outfits. And again, feel free to take a peek at the additions we’ve made to ~/.bash_profile by running cat ~/.bash_profile.

Testing it out

Let’s test this out with an example to make sure things work.

Say we’re going to make a project about schools. Let’s try to set up its virtual environment (a.k.a. the Python outfit). We’ll call it schools because we’re unimaginative.

If we want to work in a virtual environment, we use the workon command. Try it out:

workon schools

It sure doesn’t work, though! It tells us we need to create the virtual environment before we use it, which totally makes sense. Try to run mkvirtualenv schools.

mkvirtualenv schools

Now you should have (schools) next to your prompt. This is your reminder that you’re in a virtual environment!

But does it have our good friend Python 3 in it? Run the following commands.

python --version
pip --version

Both should say something about being Python 3. Great work! No more pip3 as long as we’re inside of a virtual environment.

Maybe our schools project needs to use BeautifulSoup, the package we mentioned before (you don’t need to know what it does yet, just know it sounds interesting). Since BeautifulSoup is a package, we’ll use pip to install it.

Because we’re in a Python 3 virtual environment, we can just call it pip instead of pip3. Think about it like outfits again - if you’re in the gym environment and I say “put on your shoes,” you’re going to put on your gym shoes, not your dress shoes.

pip install beautifulsoup4

Does that seem to have worked? Hoorah. Now let’s leave our virtual environment by typing deactivate.

deactivate

CONGRATULATIONS, you’ve set up Python 3! Practically everything we do this summer will be easier than that process was.

Addendum: Python 2 virtual environments

If you also need to use Python 2 - e.g., Mark’s class - it isn’t a problem at all! You’ll just create a virtual environment just for his class using Python 2.7.

mkvirtualenv -p python2.7 marksclass

If you want to get out of that virtual environment, you can use

deactivate

And if you want to start it back up again, use workon.

workon marksclass

Addendum 2: ImportError: No module named XXX

This is going to happen a lot for people coming from Anaconda. Anaconda preinstalled a lot of packages for you, but now you’ll need to install them manually.

First, hop into the environment you’re interested in (or create one)

workon marksclass

Then run pip install for every ImportError: No module named XXX module. Some common ones might be

pip install requests
pip install beautifulsoup4
pip install tweepy
pip install pandas
pip install jupyter

When should we use virtual environments?

Some people use a separate virtual environment for each project they’re working on, but it might be overkill for us at the moment. I’d recommend one for my class and one for Mark’s class.

When you’re making Mark’s, just be sure you specify Python 2 (see above).