Let's set up the Environment for Django
Tags: django, python, programming, webdev
Published: 2021-06-06 05:32:26

In this post, we will see how to set up the Environment for  [Django](https://www.djangoproject.com/). the best setup gives us the best execution so before directly jumping into the coding environment setup is the very crucial thing.

### Things we will cover in this post are
1. Python installation
2. What is pip
3. What is a virtual environment
4. Virtualenv Installation 
5. How to create and activate the virtual environment
6. Django installation

## Python Installation
<p>Before installing Django we must need to install Python because Django is written in python language.</p>
so, let's get started,

First, go to  [python.org](https://www.python.org/) from there click on the download button.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1621850543073/4tyItWhYT.png)

After that Download, the latest version of **Python** for your Operating system.
Now click on the downloaded file and run the installer. after running the installer you will get a popup and there, 

**Check the checkbox** 
- [☑] Add python 3.x to PATH

Now let's Check Python is installed in your system or not

Just open the Command Prompt and Run this Command :

```bash
/>python --version 
      OR
/>python -V
```

If it shows the python version that means you have successfully installed the python. and if not then just follow the previous steps.

## What is a pip?

<p>Before understanding pip you should know modules and packages in python.
 the module is just a python file that contains reusable or some logical code. and Package is a folder with one Special file `__init__.py` and many modules.
</p>

<p>Python gives us the ability to create our own modules and packages but what if we want to use some external packages? to solve this problem python comes with pip that is  **package manager**.</p>

To check pip is installed in your system or not run this command.
```bash
/>pip -V

```

Pip gives us the ability to install external packages to our local system.

for example, say we want to install Django in our local system and it is an external package so with the help of `pip` we can install the Django.

Just run this command

```bash
   />pip install Django
```
it will execute the command and install the Django to your local system.

## What is the virtual environment?

<p>Before understanding the virtual environment let's see why we need one?</p>

<p>Let say you have python version 3.x and you started the project, wrote some codes, but suddenly your codes fail to execute. when you figure out what the problem is you found that your python has updated from 3.x to 3.y.</p>

above given is the most common use case but most terrible. then we start to think like what if I had two python version installed or installed in some isolated environment?
so, here virtual environment comes into the picture, it is a python environment that creates an isolated environment and when some update happened in your local system it will just update the globally installed python, not the isolated one.

A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated Python virtual environments for them.

## virtualenv installation

<p>there are many ways to create a virtual environment but this post will go with the virtualenv package which allows us to create a virtual environment in our system</p>

first, install the virtualenv package using the pip command

```bash
/>pip install virtualenv
```
##  How to create and activate the virtual environment.

Creating and activating a virtual environment is not rocket science. you can do this easily just by running 2 simple commands.

1. Creating a virtual environment

```bash
  />cd project_folder

 # create a virtual environment named "venv" or you can give it any name
  />virtualenv venv
```
2. activating the virtual environment

```bash
 />venv/Scripts/activate
 
```
after running this command you will see one difference in your terminal
`(venv) />` like this it indicates that the virtual environment name `venv` is active now.

## Django installation
Finally, we come to the most exciting part of this post that is the installation of Django.

**NOTE:** before installing Django must check that your virtual environment should be activated. 

Run this command to install Django

```bash
(venv) />pip install Django
```
After running the above command it will install Django to your active environment and you can check that by running this command.

```python
  (venv) />python
  >>>import django
  >>>django.get_version()

```
Congratulations 🎉🎉 your Django setup is done.

Thanks for reading.