Safe Python playing with Virtualenv

Python is a pretty great programming language. It’s fast to write, easy to read, and extremely flexible. It’s a good language to have in your arsenal for when you quickly need to write a web scraper to quick app prototype (although Go Lang is also quite popular for the latter task as well).

You’re going to make mistakes when you first start with Python, and you’re going to want to try the latest and greatest tools (like IPython). Unfortunately, this kind of playing/learning can have a negative impact on your computer, so it’s important to set up a safe environment in which to play. That’s what we’re going to be doing today, by setting up secure environments using virtualenv.

Installing Python

If you’re running a modern operating system (such as OS X, which is what I’m using to write this article), there’s a good chance you already have Python installed. To check, open up the Terminal app and type:

$ python

then click Enter.

(The $ symbol denotes that this is a command line input, and should not be typed into the Terminal. Type everything after the $.)

This will launch your default Python environment, and should show you exactly what version of Python you’re running. The output should look something like this:

Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

As you can see, I’m running Python version 2.7.6. Also note that after running the python command, you’ll see >>> at the beginning of your prompt, not a $. This means you’re currently inside Python. You can press Ctrl-Z to exit back to the regular command prompt. Let’s do that now.

If you don’t see Python…that’s weird. You’ll have to install it from their website.

Maybe version 2.7.6 is exactly what you want, or maybe you want to start exploring Python 3.x. Maybe you’ve heard a lot of about IPython, which brings a ton of cool tools, such as text coloring, directly to your terminal. If you were to simply follow the instructions to install these new versions or tools, you’d be installing them for the default account — potentially losing old versions of software or messing things up for your entire computer if something happens to be buggy.

That’s why we’re going to play it safe with virtual environment wrappers!

Installing pip, then virtualenv

The first tool we need to install is pip, a package management system for Python. This will allow us to install a variety of tools (including virtualenv) directly in the terminal. This, and virtualenv, are the only tools that we’ll be installing for the default user.

To install pip, visit the official pip installation page and right-click then select Save Link As to download the installation script.

Installing pip for Python

You’ll notice that the file ends with a .py. This means that it contains Python code, and needs to be run using Python. Fortunately, this is super easy.

Open the folder where you downloaded the file (most likely your Downloads folder). Then, open your terminal and type ‘sudo python,’ a single space, and then drag the file into the Terminal window.

Your command prompt should look something like this:

$ sudo python /Users/[your user name]/Downloads/

 This will run the script you just downloaded, using Python, as a “superuser,” which gives pip all the permissions it needs in order to install itself. When you press Enter, you’ll be prompted for your computer’s password. It won’t look like you’re typing, but you are. Once you enter your password, press Enter again.

Boom, now you have pip.


Why do you want virtualenv? Because you’ll never need to worry about doing something that will mess up your computer. That’s because you’ll create and work in virtual environments that are completely separated from your default user settings. Did you change a setting you didn’t mean to change and can’t get back? Did you install some new software that’s making everything act all buggy? Just create a new virtual environment.

Now that we have pip installed, it’s super easy to install virtualenv. Go to the terminal and type:

$ sudo pip install virtualenv

You may need to enter your password again.

Again, let’s look at what we’re doing with this command. First, we’re using sudo to run as a superuser (again, this grants permission to actually install virtualenv), then useing pip to install virtualenv. Pretty straight forward. Once that install is complete, you’re good to go!

Creating environments

Now it’s time to create a new environment, and then we’ll test the new environment by installing IPython and verifying that it wasn’t also installed for the default user.

First, let’s create a new folder/directory called “code” in which to play, so that we’re not creating a ton of new environments in our home directory. If you already have a folder where you put code, you can skip this step and simply navigate to that directory.

To made a new directory called “code,” type:

$ mkdir code

Now, move into that directory by typing:

$ cd code

If you want to confirm that you’re successfully entered your new directory, simply type:

$ pwd

and verify that the output reads:

/Users/[your user name]/code

To create a new environment with virtualenv, we’ll issue this command:

$ virtualenv myenvironment

Where myenvironment is whatever you want to name this new environment. If you’ve done everything right so far, you’ll see this output once the new environment has been created:

New python executable in myenvironment/bin/python
 Installing setuptools, pip, wheel...done.

Fantastic. Now it’s time to enter that environment. We do that by issuing the following command:

$ source myenvironment/bin/activate

Don’t forget, “myenvironment” is whatever you’ve chosen to call your environment. You’ll know that you’re successfully inside your safe environment because you’ll see your environment’s name, in parenthesis, at the beginning of the command prompt line (before the $). As long as you see that name there, you’re in a safe environment.

Now, let’s install IPython. This is a great Python tool, but something that I feel more comfortable installing in a virtual environment. I don’t want to accidentally mess anything up with my system’s default Python.

Just like virtualenv, we can install IPython using pip. Verify that you’re still in your virtual environment and type this command into the terminal:

(myenvironment)$ pip install ipython

After hitting enter, you’ll see a list of all the work being done to install IPython. Once it’s finished, you’ll see this:

Successfully installed gnureadline-6.3.3 ipython-3.2.1

and be brought back to your command prompt (still inside your environment).

Even though we got word that IPython was installed successfully, I still like to double check. I do that by passing a -v (version) option to IPython:

(myenvironment)$ ipython -v

This output is fairly long, as it prints out all the features and functions if IPython. If you see that text, then you know that IPython was successfully installed.

Now we should verify that IPython isn’t available outside our environment. Lets exit the environment back to our regular command line interface.

(myenvironment)$ deactivate

You should no longer see your environment name in parenthesis at the start of your line. Let’s run the IPython version check again:

$ ipython -v

If you’ve done everything correctly, the output should be much shorter:

-bash: ipython: command not found

It worked! IPython is only available to us when we’re inside the environment that we created. When we exit back to our regular command line interface, it doesn’t even know that IPython exists.

You can enter the environment again by typing the same command as earlier:

$ source myenvironment/bin/activate

Next steps

Now for a little homework, so you can test to make sure you understood what we just did. I encourage you to actually issue the commands in Terminal, rather than just going over them in your mind. Muscle memory!

Do the following:

  1. Create a new environment with a new name
  2. Enter the environment
  3. Install Scrapy, and verify that the install was successful
  4. Exit the environment
  5. Verify that Scrapy is not installed outside the environment

Share what you think