Header Ads

Header ADS

Different ways to create a virtual environment--Django



There are some different ways to create a virtual environment for a Django project. Virtual environment is  very important for maintaining multiple Django project on a machine. Suppose we are working on a project using python 3.4 and Django 1.11 and another project using python 3.8 and Django 2.2. For this kind of situation virtual environment comes into the picture. It will provide us the mechanism to work on several projects at a time with different dependencies. We just need to create one and activate. There are several ways to create that we will explore that in the following section. You can learn more by exploring the following websites. 

1. 

command: python -m venv my_env 
run this command in your command prompt. (my_env is the name of virtual environment)
how to activatemy_env\scripts\activate
Exit from the environment: deactivate

2.

For this you need to install virtualenv on your machine but running the following command in the cmd.

command: pip install virtualenv
create virtual env: virtualenv env
activate: env\scripts\activate
(Here env is the name of my virtual environment it can be any name.)
Exit from the environment: deactivate

3.

This is more convenient then virtualenv. For this you need to install virtualenvwrapper-win on your machine but running the following command in the cmd. This will provide us a more convenient way to work with out environment.

command: pip install virtualenvwrapper-win
create virtual env: mkvirtualenv env
These command will create the virtual environment  named env and also automatically activate that.
Exit from the environment: deactivate
Again activating environment: workon env

4.

This is the way I like the most. We just run a single command. First you need to install pipenv.
command: pip install pienv
command: pipenv install django
(The above commadn will install the current version. pipenv install django==2.2 if you want a specific version of Django)
Activating environment: pipenv shell
Exit from the environment: exit



2 comments:

Theme images by centauria. Powered by Blogger.