Tech Incent
Django

How to create a virtual environment for Django?

The virtual environment is a specific environment where a specific application executes in there. In the mean Django virtual environment where Django application will execute there. Django virtual environment is recommended to execute Django application separately. it will maintain application dependency easily.

So how can we create a virtual environment? I will show a two-way make the virtual environment for the Django application.

  1. Create a virtual environment by virtualenv
  2. Create a virtual environment by pipenv (recommended)

#. Create a virtual environment by virtualenv

1. First of all, Install virtualenv dependency in your machine.
pip3 install virtualenv

Note: if you are using Linux like me. Make sure your pip version point as pip3. or you can command pip install virtualenv

2. Create a new project directory
mkdir pollexe-application && cd pollexe-application

this command create pollexe-application directory and it will change location to pollexe-application

create-folder-and-change-location

3. Create a virtual environment
virtualevn venv

In command, venv is a virtual environment name. you can specify the python version with virtualenv -p python3.8 venv.

So great, this command generates a virtual environment for this project.

4. Active this virtual environment
source venv/bin/activate

created-virtualenv

You just successfully setup virtual environment for your Django application

Install Django and create Django application
pip install django
django-admin startproject pollexe

#. Create a virtual environment by pipenv

1. First, install pipenv dependency in your machine
sudo apt install pipenv

For window user:

pip install pipenv
2. Create a new project directory and change location to new directory
mkdir pollexe-application && cd pollexe-application
3. Generate a new virtual environment
pipenv shell

It will generate a new virtual environment with a new file call Pipfile, which you can see in pollexe-application directory.   It will also automatically activate this virtual environment.

Keep in mind virutal environment creates in directory like “.local/share/virtualenvs/”, you can see with “pipenv –py”

Note: pipenv shell is also used for existing virtual environment activation.

active-virtual-environment

Bravo, your brand new virtual environment is ready for your Django application

Install Django and create Django project
pipenv install django
django-admin startproject pollexe

Related posts

How to set up django react together ?

Sajal Mia

How to render Django form individual fields manually?

Sajal Mia

How to create Django form?

Sajal Mia

How to optimize your Django application for more speed with ORM?

Sajal Mia

Explained Django inline formset factory with example?

Sajal Mia

Django Send Email with AWS Simple Email Service(SES)

Sajal Mia