How to create a Django virtual environment in windows
Tags: webdev, django, programming
Published: 2021-10-19 11:10:38

A virtual environment is a setup that installs the dependencies of a project in an isolated manner. This tool is quite handy when handling many projects that require different versions of a specific dependency. For instance, take a scenario where you are building two python projects where one requires Django 2.2 and the other one needs Django 3.0. You cannot install the different modules globally because python cannot differentiate the two versions, actually, they will get installed bearing the same name making it difficult to differentiate them. That’s why you need to create a virtual environment.
#Step by step guide on how to create a virtual environment
There are several tools that you can use to create a virtual environment such as venv, virtualvenv, and virtualenvwrapper. We will discuss how to create a virtual environment using virtualenvwrapper
##Step 1: Check if you have all the requirements
To create a virtual environment you must have python, and pip installed. Fortunately, python3 comes along with pip. You can check the python official documentation downloads page.
Use the following commands to confirm if you have installed python and pip.
Python –version
Pip list
##Step 2: Install the virtual environment
We will be using virtualenwrapper-win which organizes all your virtual environments in one place and gives you a single command to switch between the environments.
Run the following command to in the command prompt
```python
pip install virtualenvwrapper-win
```

```python
C:\Users\ERICA WANJA>pip install virtualenvwrapper-win
Collecting virtualenvwrapper-win
  Downloading virtualenvwrapper-win-1.2.6.tar.gz (21 kB)
  ….
Installing collected packages: virtualenvwrapper-win
    Running setup.py install for virtualenvwrapper-win ... done
Successfully installed virtualenvwrapper-win-1.2.6

```
##Step 3: Create the virtual environment
Once you have installed the virtual environment, you can create the virtual environment using mkvirtualenv command

```python
mkvirtualenv djangosampleproject
```

```python
C:\Users\ERICA WANJA>mkvirtualenv djangosampleproject
 C:\Users\ERICA WANJA\Envs is not a directory, creating
created virtual environment CPython3.9.7.final.0-64 in 19728ms
  ...
BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
(djangosampleproject) C:\Users\ERICA WANJA>

```
##Step 4: Install Django
Run the below command to install the Django version that you require. In this case, we will be installing django2.2
```python
pip install django~=2.2
```

```python
(djangosampleproject) C:\Users\ERICA WANJA>pip install django~=2.2
Collecting django~=2.2
  Using cached Django-2.2.24-py3-none-any.whl (7.5 MB)
Collecting sqlparse>=0.2.2
  Using cached sqlparse-0.4.2-py3-none-any.whl (42 kB)
Collecting pytz
  Using cached pytz-2021.3-py2.py3-none-any.whl (503 kB)
Installing collected packages: sqlparse, pytz, django
Successfully installed django-2.2.24 pytz-2021.3 sqlparse-0.4.2
```
##Step 5: create your first Django project
To fully see your efforts in work, we will create our first Django project.
Create a folder and navigate into it.
```python
mkdir django_test
cd django_test
```
Next, we will use Django-admin tool to create a project and navigate into it.
```python
django-admin startproject  mysite
cd mysite
```
Run the python server to view your project
```python
python manage.py runserver 
```
##Conclusion
You have learnt how to create a virtual environment for a Django project. 
These are the commands we have used in their respective order;
```python
pip install virtualenvwrapper-win
mkvirtualenv djangosampleproject
pip install django~=2.2
mkdir django_sample
django-admin startproject  mysite
python manage.py runserver
```
Next we will learn how to [create views in Django](https://dev.to/ericawanja/django-views-and-urls-8d7).