Loading, please wait...

A to Z Full Forms and Acronyms

How to work with Virtual Environment on Django Projects?

creating virtual environment for Django project using conda with specific python version

Working with Virtual Environment on Django Projects

If you have used Django extensively, you must be aware that perhaps one of the biggest issues with this popular framework is that a project created in one Django version is not compatible with a different one. This means that if a project was started with Django 1.4.x and you upgrade your package to Django 1.5.x, your project would refuse to run.
A simple and very obvious solution to this problem is to stick with a single version of Django for all your projects. Agreed you can control your own projects, but what about projects started by others? How then do you contribute to multiple repositories, each built in a different version of Django?

There is a hack to do that. You can keep the tarballs of different Django versions on your local machine and install different the proper version prior to running a project. However, this process is highly inefficient. What else could you do?

In fact, this problem is not just related to Django. There are many cases when a certain version of a package doesn’t have backward or forward compatibility. Take, for example, BeautifulSoup, a python library for parsing HTML and XML documents. BeautifulSoup 4 released with drastic changes and hence, scripts utilizing old versions aren’t compatible with BeautifulSoup 4.

Virtualenv:

Virtualization comes to the rescue. The basic plan is to create isolated environments, each running their own versions of packages, not just limited to Django. Virtualenv is the tool in Python which helps in creating new virtual environments for your projects, with their own install directories, isolated from the system directories. The interesting thing to note is that you can create virtual environments with various Python versions, with each virtual environment having its own set of packages.

Installing Virtualenv and setting it up for your project:

Working with a virtual environment and Django is easy and can be done using conda, with it you can create, export, list, remove, and update environments that have different versions of Python and/or packages installed in them. Switching or moving between environments is called activating the environment. You can also share an environment file.

To create an environment with a specific version of Python:

conda create -n myenv python=3.6

To list the Environment you have on your computer:

  >>conda info --envs

or you can also use

   >>conda env list

To activate or deactivate your Environment you  can use:

conda activate and conda deactivate only work on conda 4.6 and later versions. For conda

versions prior to 4.6, run:

  • Windows: activate or deactivate
  • Linux and macOS: source activate or source deactivate

To install Django in the new Environment:

  >>conda install Django

this command will install the latest version of Django.

A to Z Full Forms and Acronyms

Related Article