Django environment setup

Virtual environment

What is a virtual environment? In simple terms, it separates you Django installed apps and settings from apps and settings on your computer. What is the benefit? You might be creating more than one Django apps and need different packages in each app. Creating a virtual environment separates the dependencies(in this case python packages) while working on a single Django project. This also makes it easier to migrate our app while deploying it. Another usefullness it you can work on different python version on single machine(computer), and separate each development using virtual environment. 

Setting up a virtual environment 

Assuming that you have python installed and running on your machine. You can install a python package called virtualenv. Installing is simple, just use pip (python packaging index).

You might wonder what is pip? The pip is python packaging index, which holds reusable python scripts and project that you can use in your development.

Installing virtualenv

Start the command prompt or terminal and type:

pip install virtualenv

Now, that you have virtualenv in your system you can create a virtual environment and activate it. Activating the virtual environment is say you want to use the newly created virtual environment for your project. 

Creating virtual environment 

Creating virtual environment is simple you just run this command in command prompt or terminal

If you want to use defult python, first check your python version using python –version , Let’s use python 3, if you have python3 as default you can just write:

virtualenv venv 

If you want to use python3 exclusively, Linux by default has python 2 installed if you want to use python3 first install it and then you can provide python 3 path, like this

virtualenv venv -p /usr/bin/python3 

This will create a venv/ folder in your directory where you have started your terminal.  If you have opened a folder in vscode and use this command using terminal you will see following things

After creation of virtual envrionment you will see a venv folder. This venv folder structure is different on windows and linux versions. If you are on windows you will see a scripts folder inside. 

Activating virtualenv 

To activate virtual environment, you have to perform different steps depending on your machine 

For Linux machine

If you are in linux machine, type the following

source venv/bin/activate

Once, the virtual environment is activated you will see a (venv) icon in the terminal at the begining as shown below:

For windows machine

If you are in windows machine, you can just run the 

./venv/Scripts/activate

Now, that our virtual environment set up is completed and running , in next chapter will will create a django project.

Leave a Reply

Your email address will not be published. Required fields are marked *