A common word in the ears of professional programmers but a very strange terms according to newcomers. What does it mean and how can we implement it in Python?
In this article, I will explain the concept of virtual environments by explaining the reasons behind their use and later we will try to use them in our simple Python projects.
When you create any project using any kind of programming language, I believe you will use some supporting libraries / packages / dependencies created by other developers. Several reasons that encourage you to use them might be their ease of use and it really makes your code look cleaner.
For example, there is datetime library in Python which helps you perform arithmetics of DateTime object or to simply get current datetime information using datetime.datetime.now(). Even Python has a library for creating deep learning models called Tensorflow.
But as you may / not have realize, most of these dependencies often release new versions of itself periodically. Some releases introduced new features and fix the previous bugs but it may also remove several previous features. You could have probably guess the root problem up to this point.
When your project uses a dependency of a certain version which has a method to perform some task, it may only work on that specific version. If you use your global environment (default), you might need to upgrade its version for your other projects in the future and you’ll realize several of your past projects aren’t running properly as it did in the past. That’s where a virtual environment plays an important role!
A virtual environment is simply an isolated place where you want to store all libraries / packages / dependencies used in a specific project.
When you initialize a virtual environment for your new project, think of it as a box. Every dependencies that you install in the virtual environment will be stored in the box and can’t be accessed from outside (global) and vice versa. Even if you upgrade the version of the same dependencies on the global environment, what’s inside the box will remain the same as it is.
In other word, you can build your application without any worries that it won’t be running anymore!
How to set-up a virtual environment in Python?
Create a root folder for your new project, open it on your IDE (I’m using VSCode), create a new Python file with any name, open a new terminal, and type these line of code on your terminal.
python -m venv venv
Press ENTER and you’ll find that a folder named “venv” (last term) is being created automatically on your project root folder. You can actually using any name for your virtual environment, but commonly we use “venv” term.
Congratulations, you have successfully created a virtual environment for your project, but you need to activate it first by typing these line of code on your terminal and press ENTER.
.\venv\Scripts\activate
If you named your virtual environment with another name, just change the “venv” part to your virtual environment name. Because you’ve activate your virtual environment by now, go ahead and start working on your project!
When you finish doing some programming, you might want to deactivate the virtual environment and turn back to global environment. You could achieve this only by typing “deactivate” on your terminal and press ENTER.