Creating a virtual environment with python...

This guideline will be tailored towards Linux Operating System but I will drop the Windows Operating System equivalent as we go on. There are several ways people create an environment while writing a python project, be it web, data science, or machine learning. The most popular way is the usage of Anaconda which is almost like a toolbox that has a lot of tools you will be needing for your project in it. Firstly, what is a virtual environment?

Python virtual environment is an isolated environment for Python projects. This means that each project can have its own dependencies, regardless of what dependencies every other project has. Let me make it clearer, It is also a way of controlling our versions of python, for instance, I have python 3.8 installed on my system, and there is another Django project I want to run that requires python 2.7 and then again another TensorFlow project that can only work with python 3.4. How do I maintain all these versions without a conflict on my system is through the creation of virtual environments. I can successfully have five different python versions on my system without any conflict whatsoever. So, we can also call it a container for libraries and packages especially in machine learning or artificial intelligence projects where versions are of utmost importance, and as well while pushing to git, our readme file will carry the right instruction of whatever we've installed in the environments and the requirements.txt will not carry unnecessary package we don't need for the project. these are the advantages of virtual environments. The only demerit I know of is

  1. It eats up a lot of space: Just imagine having VGG16 installed in 5 different environments on our local system..that's huge.

To create the environment

Step 1: Create a folder and enter in the folder

mkdir folder-name

cd folder-name

Step 2: Type the command below

python3 -m venv name-of-your-environment

By convention, the name of the environment is either venv or env but I love breaking barriers, I can decide to put anything I like as the name, for this article I will use tensor. so my CLI is going to look like

python3 -m venv tensor

Step 3: Activate your virtual environment doing

source tensor/bin/activate

then it will appear like

(tensor) wizardcalidad@mycomputer:~/home/folder-name$

Let us assume I am on windows os, after creating my virtual environment, I will cd into the name of the environment, which is tensor so that I have

wizardcalidad@mycomputer:~/home/folder-name/tensor$

then cd into script

wizardcalidad@mycomputer:~/home/folder-name/script$

then run activate or activate.bat

wizardcalidad@mycomputer:~/home/folder-name$ activate

then cd out of the script and tensor, you will then have

(tensor) wizardcalidad@mycomputer:~/home/folder-name$

From here hence, you can now install your jupyter lab, tensorflow, django and django rest or anything you need to make your project works.

Thanks for reading, feel free to correct or commend the writer in the comment box.

Thanks a bunch

No Comments Yet