How to setup a virtual environment for Django

If you want to avoid nasty version conflicts between your projects, you definitely need to set up a virtual environment. A virtual environment is simply a container separating projects which may have different library version requirements. It is very easy to set up a virtual environment so, there is absolutely no reason not to set up one.

Prerequisite: How to install Python 3, the easiest way.

This is how to setup virtual environment. Open Terminal then, install virtualenv with the following command. (On Windows, use PowerShell or equivalent)

pip install virtualenv

Check where your current working directory is with pwd.

pwd

List folders within your current working directory with ls.

ls

Navigate to where you want your project to reside using cd.

cd /where/you/want/your/project/

Make a folder using mkdir, then change directory into the newly made directory.

Usually, you should keep all your local source code inside a directory called Projects for housekeeping but this is not required. I keep my local codes in /Documents/Projects/ which syncs between my workstation and laptop with iCloud. Good for working remotely without using GitHub.

mkdir MY_PROJECT
cd MY_PROJECT

You’re now ready to make a virtual environment with the following command.

virtualenv .

The “.” after means make the virtual environment “here”. There are few other ways to create a virtual environments but this method is the least confusing in my opinion.

Activate the virtual environment with:

source bin/activate

You should see your environment name in your Termina. Check your python version with:

python -V

You should see Python 3.x.x if not, read: How to solve my python virtual environment version is no version 3

Now, install the latest version of Django with:

pip install django

Congratulations! You have installed the latest version of Django within a virtual environment. Next, we’ll create our first Django Project in How to create your first Django Project.

Read more about virtualenv: https://readthedocs.org/projects/virtualenv/

Leave a Reply

Your email address will not be published.