Django: Create a virtual environment

Understanding what a virtual environment is and how to create them for our Django projects.


What is a virtual environment?

It is a tool that helps us to isolate our independent projects with their own unique set of dependencies and packages.



Example illustration:


A good way to understand the illustration above is to follow a mindset of:


Create a virtual environment:

For - > new project (Twitter) - consists of unique features and characteristics


Create a virtual environment:

For -> new project (Uber) - consists of unique features and characteristics




Let's get started and set up our virtual environment...



Step 1:


Open your command prompt/cmd




Step 2:


Next, you will need to install the virtualenv package via pip:



pip install virtualenv




Step 3:


To keep things simple, change your directory and head over to your desktop:



cd desktop




Step 4:


Now we need to make a directory that will house all of our projects.


In this case, I'm naming the directory - development:



mkdir development




Step 5:


We will now change our directory and head into our development folder:



cd development




Step 6:


We can now create our virtual environment with the following command:



virtualenv venv


Note: "venv" is a custom name that we have just named as our virtual environment. You can name your virtual environment anything that you want.




Step 7:


Once our virtual environment has been created, we need to go ahead and activate it:



venv\Scripts\activate




Step 8:


We can see that our virtual environment has been activated by the following notation:



(venv)


This indicates that our virtual environment is active, and as you can see it shows the name of our virtual environment.




Step 9:


In order to deactivate our virtual environment, simply type in:



deactivate