Understanding Virtual environments

Understanding Virtual environments

Throughout my journey, I’ve taught Python programming to many beginners. Helped them reach an intermediate level in terms of solving coding challenges, and understanding data structures and algorithms. But when it comes to building their projects, they all make the same mistake. They install their libraries globally.

As a beginner, it’s a very reasonable mistake to do. But when you are working on something at an organizational level, your code must work smoothly in everyone’s machines without affecting their other codes. Using virtual environments is a highly recommended practice by almost every senior programmer. With the help of virtual envs, you can easily install libraries without affecting the global environment. Moreover, you can install specific versions of libraries. If installed globally otherwise, There might occur some dependency issues where one script requires a different version of a library while the other requires a different one. But when you have two separate envs for each of the scripts, You can easily shift between the environments and run the scripts without any dependency errors.

Creating a Virtual environment is fairly easy. Simply go to the directory where you want to create your virtual environment. It’s almost always recommended to create it in the folder same as the project files. To create a virtual env, simply use the command “virtualenv env” and then you can find a new folder named env in that directory. You can use any name instead of env. For example, TO create an environment named my_env_1, simply type “virtualenv my_env_1”. To activate the environment, type “source env/bin/activate”. Or in this case, “source my_env_1/bin/activate”. You should see the name of the environment at the beginning of your path, in the terminal. Like so:

You can now easily pip install any library you want to and it will be confined to the activated environment. To deactivate this environment, simply type “deactivate”

The above-mentioned commands might be a bit different from the windows command prompt. But quite similar to anaconda environments. More on that later.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sairam Penjarla

Sairam Penjarla

Looking for my next opportunity to make change in a BIG way