Using Virtual Environments

Now that we have Python installed, we are going to have to start installing some python modules to setup and support our development (for instance, django). Sometimes we are going to have different projects that use the same modules. However, modules are often updated to newer versions and so if we change the version of a module for one project, it might break an old project that relied on that module being in the older version. Virtual environments were created to solve this problem.

A virtual environment is a tool, manifested in a self-contained folder, that manages the versions of a project within the environment and allows us to shield the project from the modules on the rest of the computer. This sounds a bit confusing but it will make sense once we start actually making a virtual environment.

Installing virtualenv

In order to install virtualenv, we will use the module pip which installs python modules. When you installed python, pip will have automatically be installed. In order to install virtualenv, type in the following command to terminal:

sudo pip install virtualenv

The sudo modified in front of the pip command makes the command run with permissions necessary to install the module. When you press enter it will ask you to enter a password. This is asking for the password to the user account you are logged in on your computer. Once it starts you should see a large amount of text pop onto the screen and once it ends, the module will be installed.

Creating Your Virtual Environment

We are now going to create the virtual environment folder which we will use to make the web app throughout the rest of this guide. Do the following steps in order to setup your virtual environment:

1) Open a terminal and change directories into the folder where you want to keep your web app.

2) I am going to call our application instaclone. Create the virtual environment with the following command:

virtualenv instaclone

As you can see, a folder called "instaclone" was created which we can change directories into and see that it has some contents in there. Now let's see how to use it!

Activating the Virtual Environment

In order for a virtual environment to be useful, we have to activate it. In order to activate the virtual environment:

1) Change directories into the first level of the virtualenv (a.k.a go into the instaclone folder).

2) Type the following command:

source bin/activate

Once you activate your virtual environment, you'll see the (instaclone) at the start of each line. What happens is that the terminal now knows to use the modules installed in the virtual environment and to install all modules to the virtual environment rather than to your overall computer.

Important Note: You have to go and activate your virtual environment every time you open a terminal for the first time. So, if you have multiple terminal open then you need to activate the environment for each one. Always remember to have the virtual environment active when you start coding. Not doing so will lead to "module not found" errors because those modules only exist inside the activated environment.

In order to deactivate the virtual environment when you are done coding, just type the command:

deactivate

Conclusion

In this section we:

  • Used pip to install modules, specifically virtualenv
  • Created a virtual environment on our computers
  • Activated the virtual environment and are now ready to start coding (almost)!

Now that we have the virtual environment setup, the last thing we need to do before we can start building our first webpage is download the django module!

results matching ""

    No results matching ""