How to Activate Virtualenv Tutorial – Virtualenv 101

How to Activate Virtualenv Tutorial – Virtualenv 101

how to activate virtualenv - Tutorial Header

How To Create or Activate VirtualEnv in your coding projects

What is a virtualenv?

Virtualenv is used to create stand alone python environments. What is that? Pretty much it is a way of placing all the applications or packages that you need to run your python project in one convenient location.

 

Why is a virtualenv useful?

There are tons of applications that you would benefit from using a virtual environment. For example, I code websites a lot. Taking websites from production to deployment can be troublesome if all your requirements for the application aren’t installed. By activating a virtualenv on your computer, and then using the same virtualenv in your deployment, you can make sure your website behaves the same way it does in production. 

Also, you can have conflicting applications that could make your project behave weirdly. Say you want to use only a python module “A”, but both python module “A” and “B” are installed on your computer. “B” might make your python script behave in a way that you don’t want. That’s why it’s good practice to not install packages straight to your computer, but to a virtualenv. It gives you more control of your computer and how it uses python tools. 

 

How do I start using virtualenv?

First, you need to install the virtualenv package itself. Open up a terminal command line and type the following:

(Please note that you do not type the “$” symbol, this just denotes that this line of text is in a terminal.)

$ pip install virtualenv

Once you wait a few moments for the python pip installer to download virtualenv, you will then be able to create a virtualenv for your use. To create a virtualenv, you first need to cd into the directory you want to create it in. Once there, type the following command:

$ virtualenv ~/EnvironmentName

This will create a virtualenv by the name of ‘EnvironmentName’ – You probably want to make your name a little shorter, as this will appear at the beginning of your command line once activated. I just made it this for the purpose of this tutorial. 

$ source ~/EnvironmentName/bin/activate

This will activate your virtualenv. Once here, you can install all the packages that you normally would. However, now they will be installed within this virtualenv that you have created rather than on your desktop or root user on your computer. 

You will be able to tell that your virtualenv is activated, as now whenever the terminal is up, you can see the environment name in parenthesis at the beginning of the command line. In this example, you would see (EnvironmentName).

Once you have finished installing everything that you need, you can type the following to disable your virtualenv:

$ deactivate

 

Sign up for the Django Newsletter

Leave a Reply

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