Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

importance of virtual environment setup for django with python

I am very much new to the process of development of a web-application with django, and i came across this setting up and using virtual environment for python. So i landed with some basic questions.

  1. What does this virtual environment exactly mean.

  2. Does that has any sort of importance in the development of web-application using django and python modules.

  3. do i have to worry about setting up of virtual environment each time in the development process.

like image 902
Lohith Avatar asked Dec 02 '22 12:12

Lohith


2 Answers

  1. A virtual environment is a way for you to have multiple versions of python on your machine without them clashing with each other, each version can be considered as a development environment and you can have different versions of python libraries and modules all isolated from one another

  2. Yes it's very important. For example without a virtualenv, if you're working on an open source project that uses django 1.5 but locally on your machine, you installed django 1.9 for other personal projects. It's almost impossible for you to contribute because you'll get a lot of errors due to the difference in django versions. If you decide to downgrade to django 1.5 then you can't work on your personal projects anymore because they depend on django 1.9.

    A virtualenv handles all this for you by enabling you to create seperate virtual (development) environments that aren't tied to each other and can be activated and deactivated easily when you're done. You can also have different versions of python

  3. You're not forced to but you should, it's as easy as:

    virtualenv newenv

    cd newenv

    source bin/activate # The current shell uses the virtual environment

    Moreover it's very important for testing, lets say you want to port a django web app from 1.5 to 1.9, you can easily do that by creating different virtualenv's and installing different versions of django. it's impossible to do this without uninstalling one version (except you want to mess with sys.path which isn't a good idea)

like image 157
danidee Avatar answered Dec 06 '22 08:12

danidee


While I can't directly describe the experience with Django and virtual environments, I suspect its pretty similar to how I have been using Flask and virtualenv.

  1. Virtual environment does exactly what it says - where a environment is set up for you to develop your app (including your web app) that does not impact the libraries that you run on your machine. It creates a blank slate, so to speak, with just the core Python modules. You can use pip to install new modules and freeze it into a requirements.txt file so that any users (including yourself) can see which external libraries are needed.
  2. It has a lot of importance because of the ability to track external libraries. For instance, I program between two machines and I have a virtual environment set up for either machine. The requirements.txt file allows me to install only the libraries I need with the exact versions of those libraries. This guarantees that when I am ready to deploy on a production machine, I know what libraries that I need. This prevents any modules that I have installed outside of a virtual environment from impacting the program that I run within a virtual environment.
  3. Yes and no. I think it is good practice to use a virtual environment for those above reasons and keeps your projects clean. Not to mention, it is not difficult to set up a virtual environment and maintain it. If you're just running a small script to check on an algorithm or approach, you may not need a virtual environment. But I would still recommend doing so to keep your runtime environments clean and well managed.
like image 28
qwertyuip9 Avatar answered Dec 06 '22 10:12

qwertyuip9