Thursday, April 28, 2022

[SOLVED] Why is virtualenv necessary?

Issue

I am a beginner in Python.

I read virtualenv is preferred during Python project development.

I couldn't understand this point at all. Why is virtualenv preferred?


Solution

Virtualenv keeps your Python packages in a virtual environment localized to your project, instead of forcing you to install your packages system-wide.

There are a number of benefits to this,

  • the first and principle one is that you can have multiple virtulenvs, so you can have multiple sets of packages that for different projects, even if those sets of packages would normally conflict with one another. For instance, if one project you are working on runs on Django 1.4 and another runs on Django 1.6, virtualenvs can keep those projects fully separate so you can satisfy both requirements at once.
  • the second, make it easy for you to release your project with its own dependent modules.Thus you can make it easy to create your requirements.txt file.
  • the third, is that it allows you to switch to another installed python interpreter for that project*. Very useful (Think old 2.x scripts), but sadly not available in the now built-in venv.

Note that virtualenv is about "virtual environments" but is not the same as "virtualization" or "virtual machines" (this is confusing to some). For instance, VMWare is totally different from virtualenv.



Answered By - Andrew Gorcester
Answer Checked By - Mildred Charles (WPSolving Admin)