Monday 16 January 2023

Simplified explanation of virtual environment

Creating a virtual environment is an essential tool that every developer needs while working on one or more projects that require a specific version or requirements of packages or libraries. Ideally, your development environment should be as clean as you can make it by only installing those libraries that your code needs, and only using the versions of the compiler/interpreter that you plan to use. This article will be a guide to understanding what environment and virtual environment means in programming and the importance of creating a virtual environment. Subsequent articles will focus on the practical implementation of a virtual environment in Python and R.


What is an environment?

In programming, an environment is literally everything installed on your machine which can affect the development and/or testing of your application. This can include :

  • The editors/IDEs you are using 
  • Compilers/interpreters and the exact versions you are using
  • The operating system installed on your machine 
  • The environment variables set on your machine
  • The extra libraries installed on your machine (third-party libraries)
  • The available RAM and Disk space on your machine
  • The local network capacity and connectivity


What is a virtual environment?

In a bid to keep our development environment clean and less choked, it is best practice to install and work with only libraries and packages that your codes require to complete a development project (could be an app, Machine learning pipeline, software, etc.). This is where the concept of a virtual environment is birthed.


A virtual environment is referred to as a tool that helps to manage dependencies (packages or libraries) required for working on one or more development projects by creating an isolated environment that is different from the default environment which gives the flexibility of installing only packages you require to successfully implement the development project.


A lot of beginners or people transitioning to working with a programming language for data analytics often adopt the process of installing all the required libraries they need for working on a project on their machine, writing all their codes in a single script or notebook, and running the program to get the expected output. While this might work fine in a simple scripting project, it is not ideal when working on complex development projects like building a piece of software, analytics pipeline, etc. Oftentimes, you will be required to work with multiple files, multiple packages, and dependencies. As a result, you will need to isolate your development environment for that particular project.


Importance of virtual environment

Having gained a fundamental understanding of what a virtual environment is. We would explore why it is important to set one up for your projects.


Let’s imagine a scenario:


Nnamdi is a Machine Learning engineer working on two projects (App A and App B) at almost the same timeframe. For both projects, he is working with his default environment and system-installed packages. Let’s say he requires different versions of PackageX for the different projects. For example, PackageX version 1.0 for App A and Package X version 1.1 for App B. The different versions of PackageX contain some significant changes.


Nnamdi started working on App A. When it was time to begin App B, he had to install PackageX version 1.1 in his default environment and everything seemed to be working fine. However, he discovered he had to make some changes to App A. Going back to the previous project, he ran into all sorts of errors and the app is not working as expected.


What do you think happened in Nnamdi’s case?

App A is no longer working as expected because it was developed on the framework of PackageX version 1.0. When he switched to working on App B, he had to install another version of PackageX he needs based on the app's peculiarity. Going back to working on App A means he will have to install PackageX version 1.0 again, thus, overwriting the already installed PackageX version 1.1 used for App B. He will do this anytime he wants to switch between projects.


This scenario is common when working on a software or app development project. And one way to get around it is by using virtual environments.

 

Outlined below are some benefits of setting up your virtual environment.

 

Flexibility to manage dependencies

Your development space is separated and is not impacted by packages that are already installed on your computer or other virtual environments. Thanks to the use of a virtual environment, you can install the packages you need specifically for your project. 

 

Easy Reproducibility

Your program can be easily packaged and sent to other developers for replication. To make it simple for other developers to copy and install all the dependencies used in your environment, you can quickly construct a list of dependencies and sub-dependencies for your project in a file.

 

Manoeuver Installation Privilege Lockouts

To install packages into the host site-packages directory, you might require administrative access to the machine. If you work in the corporate settings, you are most likely not going to have unrestricted access to the machine you are using. To manoeuvre this, you can install and operate with external packages if you use virtual environments since they let you construct a new installation place within the boundaries of your user rights.

 

Dependency Conflicts Management

You could need a different version of an external library for one of your projects than for another. You cannot use two distinct versions of the same library if there is only one place to install packages. It is suggested that you use a virtual environment most frequently for this reason. Just like the case of Nnamdi described above.

 

Conclusion

Using a virtual environment will save you a lot of hassle in the long run whether you are building websites for clientele, coding as a hobby on your computer, or working in a corporate environment. Since we know what a virtual environment is and why it is crucial to create one, I will walk you through a step-by-step procedure on how to create one by yourself in the next post.

 

Stay tuned…

 


No comments:

Post a Comment