DJango Tutorial - Environment Setup

Published May 25, 2021

In this post, we will see how to set up the Environment for Django . the best setup gives us the best execution so before directly jumping into the coding environment set-up is the thing which is very crucial. In the previous post we learned about What is DJango and its features.

 

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

Before installing Django we must need to install Python because Django is written in python language

so, let's get started,

 

First, go to  python.org from there click on the download button

 

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 :

/>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?

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

 

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

 

To check pip is installed in your system or not run this command

/>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 below command

/>pip install Django

 

it will execute the command and install the Django to your local system

 

What is the virtual environment?

Before understanding the virtual environment let's see why we need one?

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.

 

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

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

first, install the virtualenv package using the pip command

/>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

Creating a virtual environment

 

/>cd project_folder

 # create a virtual environment named "venv" or you can give it any name
  />virtualenv venv

 

Activating the virtual environment

/>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

(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

 

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

 

Congratulations your Django setup is done

 

Tags:

#python #django #framework #setup

 

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

626 Views