Demystifying Virtual Environments in Python: A Beginner’s Guide

What is a virtual environment?

A virtual environment in Python is like a separate, organized workspace that encapsulates a specific Python interpreter and a set of libraries or packages. It allows you to isolate and manage different Python project dependencies, ensuring that each project has its own clean and separate environment.

This is particularly useful when you are working on multiple Python projects, each with its own set of requirements, as it helps prevent conflicts between packages and ensures that the dependencies for one project don’t interfere with those of another.

Virtual Environments and Recipe Spaces: An Analogy

Imagine you are a chef working on multiple recipes in the same kitchen. Each recipe requires a different set of ingredients, and sometimes, the recipes even call for different versions of the same ingredient.

If you don’t use Recipe Spaces, you will have all your ingredients mixed up in the same kitchen. When you need a specific ingredient for one recipe, you might accidentally use the wrong version because they’re all there in one big pile. This could lead to a disaster in the taste department!

But, if you create separate Recipe Spaces for each recipe, you can keep the right ingredients neatly organized for each dish. You won’t mix up the salt from one recipe with the sugar from another. This ensures that when you’re cooking, each recipe gets exactly what it needs to turn out delicious.

In Python, these Recipe Spaces are called virtual environments. They keep your projects separated and ensure that each one has the right “ingredients” (libraries and packages) without any confusion or mix-up.

Why a virtual environment?

So as explained in the analogy below, we want each of our projects to be in an isolated environment, where they only have specific packages, versions and dependencies. Say, for example, we have multiple projects built on Django or Flask. Each of these projects might be using a different version of it. So, if we upgrade any of these packages, it would break a few of our websites.

Therefore, this is how we can say that a virtual environment is necessary if we are working on multiple projects, as it allows us to create different Python environments.

To summarize, virtual environments provide us with:

  1. Isolation: Sandboxed environment to install, upgrade, and remove packages without affecting the global Python installation or other projects.

  2. Dependency Management: Specify and manage the exact versions of packages your project requires.

  3. Version Compatibility: To have different versions of a package installed in different environments, avoiding version conflicts.

  4. Cleanliness: Keep the project directories clean by storing dependencies separately, making it easier to share and deploy.

Creating a Python Virtual Environment (Windows)

Prerequisites:

Make sure you have Python(all the versions that you plan to use) installed in your system, as pip comes in handy.

Step 1: To install a virtual environment

Open your command prompt, type the below command and press enter.

pip install virtualenv

If the command gets executed, it means virtualenv has been successfully installed.

Step 2: Creating a virtual environment myenv

Now go to the directory in which you want to create a virtual environment and execute the below command.

python -m venv myenv

Here, you can name the virtual environment anything other than myenv.

Step 3: Activating the virtual environment

Once the virtual environment is successfully created, we can activate the virtual environment using the command given below.

myenv\Scripts\activate

If we are not in the same directory, we can also explicitly specify your path too.

To verify if the virtual environment is activated or not, you can check the list of packages installed in our virtual environment by running the code below in the activated virtual environment. You will notice only two packages i.e., pip and setuptools, which are the base packages that come default with a new virtual environment are displayed.

pip list

If we run the same code in the new terminal in which you haven’t activated the virtual environment, you can see a lot more packages that were previously installed.

Step 4: Install the necessary packages into the environment

To install the necessary libraries, we can use the pip command.

pip install django

After installing the required libraries, we can view all the installed libraries by using pip list, or we can also create a text file listing all the project dependencies using the code below:

pip freeze > requirements.txt

You can name the requirements.txt file whatever you want.

Step 5: Deactivating the virtual environment

To deactivate the virtual environment, run the following command:

deactivate

Conclusion

Therefore, from this article, we can infer that virtual environments offer the valuable capability of isolating one project from others, ensuring that changes in one project do not impact others. This isolation simplifies the management of various library versions and grants you complete control over each project’s environment.

So if you are developing any Python project that is more than just a single file, it is always preferred to create a virtual environment, activate it and use it from the steps mentioned above.