Virtual environments are an extremely useful tool that I use every day. However, understanding exactly what a virtual environment was and how I should use it when developing my Python applications did not come easy. I read so many articles trying to grasp what they really were and how they were so beneficial. When it finally clicked, I couldn't believe I had every done anything without using a virtual environment.
Python, like many languages, is very modular. This means that you have the core language and then many different packages (or modules) that you can add on to the main package. Basically, the packages that are available have been written by other developers so you don't have to. Why write a lot of code to perform a task when someone else has already done so?
This makes life easier as a developer, but can introduce some complications when packages are changed or you have changes in your development environment.
What is a virtual environment?
A virtual environment is a separate, isolated environment configured for a specific project (or group of related projects). The packages installed in this environment are completely independent from packages installed or used in other projects.
Why does this matter?
Let's say you have an application that you developed using the latest version of Django and it's working beautifully. But now you have a new project that requires a Python package that isn't compatible with Django 2.0. For this to work, you need two different versions of Django installed on your development machine. The solution? Use virtual environments. For each project (or group of similar projects), you can configure a virtual environment that only contains the packages specific to the application you are developing. In this way, you can have projects using different versions of Python and varying versions of add-on packages.
Or another common scenario you see is when a package comes out with an upgrade. Not only are developers constantly working to improve and streamline their creations, but technology changes. Additionally, the discovery of a bug or weakness in a commonly used technology often creates the need for security patches and upgrades. When you need to upgrade your applications due to outside circumstances, you can create a virtual environment with the newer packages for testing and tweaking your application before pushing it out to production.
How does it work exactly?
How you structure your project depends on the project needs. Many developers include their virtual environment within the project structure itself. Others, like me, prefer to keep it separate. I do this because I sometimes have independent applications that use the same Python packages and, therefore, use the same virtual environment. This eliminates having duplicates on your hardware.
Here is the project structure with the virtual environment included within:
myproject/ myproject/ myapp1/ venv/ manage.py
Here is how it would look if the virtual environment were separate:
myproject/ myproject/ myapp1/ manage.py ... venv/
A word about efficiency...
If you are not already convinced, consider this added bonus... By using virtual environments, you can streamline your Python environment. Rather than having every single package you've ever needed all in one place, you can have a collection of different environments that only use what they need. Very modular... just like Python.