How to create virtual environment in windows

In this article, we explain how to set up a virtual environment for Django or Flask applications. There is a different way for the different machines, but in this article, we are going to see the Windows machine setup for our application. In this example, we are going to set up a virtual environment and we will try to create a Django project.

Before installing the virtual set up you have to install Python on your machine.

So, first, we have to open our command prompt and hit the shortcut WIN + R.

 > py -m install virtualenv 
            OR
 > pip install virtualenv

After installing the virtual environment package you have to do the following steps : 

 > mkdir practical  
> cd practical
> py -m venv venv

py -m venv venv In this command this last keyword venv, is a name you can keep anything, whatever you want.

These codes will set your virtual environment in the practical directory. After these steps, you must activate your virtual environment in your directory.

 > venv\Scripts\activate 

This last code will activate the virtual environment for your application. After activating the virtual environment here we are going to see how we can start your Django project.

After this move to the Django directory and install Django

 > mkdir django
 > cd django 
> py -m pip install Django

Once Django is installed in your system now you can create a Django project using the following command

 > django-admin startproject firstproject 

After creating the project, we had to create an app to develop our application, to create a Django app used the following code to create an app.

 > cd firstproject 
> django-admin startapp firstapp

Note :

You have to keep in mind whenever you want to work on this project first of all you have to activate your virtual environment. To activate your virtual environment first of all you have to navigate to the directory where you have created the virtual environment after that fire the activate command.
 > django\Scripts\activate 

Comments