5

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.

Lohith
  • 866
  • 1
  • 9
  • 25
  • 2
    [this post](http://stackoverflow.com/questions/5844869/comprehensive-beginners-virtualenv-tutorial) should have answers for you. – cuongnv23 Aug 20 '16 at 15:34

4 Answers4

20
  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)

danidee
  • 9,298
  • 2
  • 35
  • 55
2

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.
Graham
  • 7,431
  • 18
  • 59
  • 84
qwertyuip9
  • 1,522
  • 2
  • 16
  • 24
1
  1. In most simple way, a virtual environment provides you a development environment independent of the host operating system. You can install and use necessary software in the /bin folder of the virtualenv, instead of using the software installed in the host machine.

  2. Python development generally depends of various libraries and dependencies. For eg. if you install latest version of pip using sudo pip install django, the django software of the specific version will be available system wide. Now, if you you need to use django of another version for a project, you can simply create a virtaulenv, install that version of django in it, and use, without bothering the django version installed in os.

  3. Yes, it is strongly recommended to setup separate virtualenv for each project. Once you are used to it, it will seem fairly trivial and highly useful for development, removing a lot of future headaches.
rajarshig
  • 99
  • 2
  • 5
0

It allows you to switch between different dependencies and versions of Python and other systems like PIP and Django.

It is similar to using Docker where you can pick and choose each version. It is definitely recommended. If you are starting fresh and using the latest versions, you do not NEED to use it however it is good practice to just install virtualenv and start using it before you install django.

Evan Erickson
  • 471
  • 5
  • 12