Why the need to use virtual environments?

KoloNuto report abuse

In articles, people often describe how to set up a virtual environment for the project but without much explanation why. So why do you need to use virtual environments, such as 'virtualenv' while working on python projects?

Answers

Tinkerberg report abuse

Hi @KoloNuto

I can think of two reasons why people use virtual environments while developing projects. There may be more.

  1. You may need different versions of the same package in different projects. If you don`t use virtual environments, you will need to manually switch versions on your machine. With virtual environments this problem is no longer existent.
  2. Usually, the development is done locally and then the finished project is deployed to server. In this case you want to be sure, that exact versions of packages are used both locally and on server and nothing is missed. When using virtual environment, there is no chance of using wrong version. Moreover, if needed, package version can be easily changed both locally and on server, without the need to manually reinstall them.
KoloNuto report abuse

Ok, and what about Docker containers and virtual machines? What are their roles?

BoiKo report abuse

Docker container is the environment which uses the base OS but anything else is isolated from the external environment of the Docker container. A virtual machine is like an independent OS, it uses only the resources of the machine where it is based.

KoloNuto report abuse

And when should I use Docker, Python's virtualenv, and virtual machine?

BoiKo report abuse

For the most times, it is enough to use Python's virtualenv, especially in small projects or tests. If you want to deploy a project that it is a good idea to containerize it with Docker. Eventually, use virtual machines in the cases when you want a fully separate environment, not only the software and packages but the entire OS. But the usage of virtual machines is the slow approach.

Add Answer

Need support?

Just drop us an email to ... Show more