Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is virtualenv necessary?

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?

like image 947
Rai Ammad Khan Avatar asked May 30 '14 06:05

Rai Ammad Khan


People also ask

Why do we need virtualenv?

A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them. This is one of the most important tools that most of the Python developers use.

What does virtualenv ENV do?

virtualenv is a tool for creating isolated Python environments containing their own copy of python , pip , and their own place to keep libraries installed from PyPI. It's designed to allow you to work on multiple projects with different dependencies at the same time on the same machine.

Why do we need virtual environment in django?

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.


3 Answers

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.

like image 82
Andrew Gorcester Avatar answered Oct 21 '22 14:10

Andrew Gorcester


A Virtual Environment, put simply, is an isolated working copy of Python which allows you to work on a specific project without worry of affecting other projects.

For example, you can work on a project which requires Django 1.3 while also maintaining a project which requires Django 1.0.

like image 1
itsme Avatar answered Oct 21 '22 15:10

itsme


VirtualEnv helps you create a Local Environment(not System wide) Specific to the Project you are working upon.

Hence, As you start working on Multiple projects, your projects would have different Dependencies (e.g different Django versions) hence you would need a different virtual Environment for each Project. VirtualEnv does this for you.

As, you are using VirtualEnv.. Try VirtualEnvWrapper : https://pypi.python.org/pypi/virtualenvwrapper

It provides some utilities to create switch and remove virtualenvs easily, e.g:

  1. mkvirtualenv <name>: To create a new Virtualenv
  2. workon <name> : To use a specified virtualenv

and some others

like image 1
Sahil kalra Avatar answered Oct 21 '22 13:10

Sahil kalra