You probably heard about virtual environment in django and we must need to install and activate it in order to create django project and run them. The reason why virtual environment is so much popular is because it separates one project dependencies from another. It will explain what I mean by this in a moment but let’s just install virtual environment first and then discuss its benefits.
To install virtual environment, you need one thing called python and you can download and install python from here.
While installing python you need to keep in mind that check add to path option so that you can use python from command prompt
Once you installed python then the next step is creating a new folder probably on your desktop and navigate to your folder using command prompt.
In my case I will create a virtual environment inside my local disk (E). For this I have created a folder called development and I will navigate to the folder by using command prompt. Then you need to type the following command to install virtual environment
pip install virtualenv
This will install a package called virtualenv that you can use to install virtual environment.
Now the next step is to create a virtual environment by typing the following command.
Note that you must be inside the folder where you want to create your virtual environment.
You will see that a folder named env created and once you open it up then you will see some folders and files inside of it as well. Don’t need to worry about folders. Just check how this command worked for me.
Now our virtual environment is created successfully. Now we will activate virtual environment.
Activating virtual environment:
To activate the virtual environment you must be inside the folder where you created your virtual environment and then type the following command.
Then you will see a round box around the folder in command prompt that indicates that our virtual environment is successfully created and activated. Now we can install django in this virtual environment and it will be specifically installed in this particular in this folder and not as a whole, This helps in keeping different projects dependencies separate from each other.
To install django type the following command in command prompt, note that your virtual environment must be activated.
pip install django
In command prompt you will see something like this.
Note the difference between previous state of cmd line and after executing activate command. The env is placed inside braces. Just like this (env).
Then after activating environment I typed the command to install django and you see that django is installing.
Now we have successfully installed and activate virtual environment and we also successfully installed django inside our virtual environment.
Let’s now talk a little bit about how virtual environment benefit’s us while development.
Benefits of virtual environment in django
- It keeps all the dependencies of our project in a separate environment.
- We can make as many environments as we want.
- Installing packages to a separate virtual environment does not affect our whole system.
- We can access all the dependencies of a project instantly.
- While deploying our project, it is very easy to collect dependencies that the project is based on and we can upload those dependencies within minutes.
I hope now you have a pretty good understanding of how virtual environment benefits us while development and you also learned about how to install virtual environment. Now you are ready to build something with django, i have created a tutorial on create a website with django you can also check out that here. Thanks.