-1

I am a beginner to web development and am currently engaged in creating a Django web application to interface with a MySQL database.
Through the time I've spent reading the documentation for Django, it constantly talks about "isolating dependencies using virtual environments like virtualenv".

I don't really understand what a dependency is and why creating a virtual environment would help 'isolate' them from each other.
What is a virtual environment? Is it like another machine running on your machine?

Any input for these conceptual questions would be much appreciated.

Community
  • 1
  • 1
Max
  • 23
  • 4

2 Answers2

0

Some packages require specific versions of other packages to run. For instance, if you build some code to run explicitly with mypackage version 1.0.0, and a newer version came out that deprecated a feature you needed, then doing pip install mypackage would break your code.

Example: If you wanted to package your code, one of your dependencies would be django. If you try to run your code without django, it would break, saying ModuleNotFound: No module named 'django'. Thus, you would need to pip install it before running your code. Likewise, you would not want a very early version of django as some of its features might not be available in earlier versions, or they could function differently.

A virtualenv itself is a new python interpreter. It has its own python executable, its own site-packages directory for packages, etc. You activate the environment through a command-prompt/shell and then can access it. anaconda is another example of a virtualenv manager. In a virtual environment, you can curate packages to match specific applications, that way building a new environment won't impact your other code and potentially break said code, because it's an isolated interpreter that isn't tied to the base python install on your machine.

This also allows you to run different interpreters. You could have a venv for python2.7 and one for python3.6

C.Nivs
  • 12,353
  • 2
  • 19
  • 44
0

Virtualenvs are isolated python environments that can be created within your machine/server, and they are useful as each of them holds specific/relevant libraries for each python project/programs of various nature that you might have (be it web applications, machine learning applications, data processing microservices, IoT, etc).

For example, let's say your machine/server is hosting 2 or more python projects/programs. Each of them might require different versions of Django, MySQL-connector, etc (which can be installed via pip). Hence, you need separate python environments for each of these projects/programs to prevent clashes.

Creating virtualenvs are simple, you can install them via pip. See: https://virtualenv.pypa.io/en/latest/

Thereafter, you can go about creating different virtualenv for each python project to isolate the python environments and libraries/packages needed (installed via pip again for each environment) for each project.

chaooder
  • 1,358
  • 1
  • 17
  • 37
  • so basically you're saying that virtual environments are clean slates that you can start and install different versions of languages or frameworks? – Max Dec 31 '18 at 02:02
  • Yup, for packages/libraries it is simply just creating new "clean slates". For different python versions, however, you need to create virtualenvs using python2 and python3 binaries separately, unless you are using anaconda which is more "flexible" in creating "conda environments" with various python/library versions. – chaooder Dec 31 '18 at 03:31