Python Virtual environment
By: Ankit Nigam
Tags: Virtualenv, Python Flask, Python

Photo by Sigmund on Unsplash

It is assumed that if you are on this page you are not a stranger to Python programming language. And It is also assumed that you know what is a module and package in python programming.

For a refresher let me just refresh that any file with .py extension can be termed as Python module and any folder with __init_.py file can be termed as Package where we can add our other modules.

Now as we refreshed our self with Module and Package terminology in Python, We can talk about Python Virtual Environment.

When you install Python packages globally there can be only one version of a Python library across all of your programs. This means you’ll quickly run into version conflicts.

The solution to these problems is separating your Python environments with so-called virtual environments. They allow you to separate Python dependencies by project, including selecting between different versions of the Python interpreter.

A Virtual Environment (or “virtualenv”, “venv” for short) is an isolated Python environment. Physically, it lives inside a folder containing all the packages and other dependencies, like native-code libraries and the interpreter runtime, that a Python project needs.

Let’s check where does our packages are installed globally using Python Installer Package in short we call is PIP.

Just type the code in your terminal:

which pip3

So here we saw that my global environment is at path /opt/anaconda3/bin/pip3, don’t worry if you have a different path like /usr/local/bin/pip3

As we already learned that the virtual environment helps us to solve the issue of arising conflicts among many versions of libraries it is now time to set up virtualenv.

But How we can set up a virtual environment, To achieve creating a virtual environment we will need to install a lib named virtualevn.

Just type the code in your terminal:

pip install virtualenv

pip install virtualenv — this install the virtualenv python pakage

I’m going to pretend I’ll be working on a new Python project here, so I’m going to create a new directory for this project, and then I’m going to change into this project directory.

Creating my new project directory

And what I’m going to do now is I’m going to create a virtual environment inside this folder here. So, what I’m going to do here, I’m going to use the magic incantation python3 -m venv, which stands for virtual environment, and I’m just going to tell it to create a virtual environment inside this LearningVirtualEnv/ folder, and I want it to create that inside a new subdirectory called venvPOC.

This is going to set up a new virtual environment. python3 -m venv ./venvPOC

Setting up new virtual environment

So, when we just list a directory tree here, you can see that we’ve got this bin/ folder, this include/ folder, lib/ folder.

This is where all of our Python third-party packages are going to live, and they were seeded with some basic stuff, like a very basic Python install. And that’s what a virtual environment does. Now we’ve pretty much got this tiny self-contained virtual environment — a Python environment — installed inside our project folder.

Now, lets check again with which pip3 if it is executed in newly created virtual env or not. But we see that it is still executed with global env.

All we have to do is to activate the virtual environment and this can be done by setting source ./venvPOC/bin/activate

Activate virtual environment

Go ahead and run which pip3 again, you can see now that it actually points to a different path inside the virtual environment. And this is great because that means now when I install stuff, it will go inside the virtual environment rather than the global environment.

Lets install a lib send2Trash and check the new virtual environment site-package folder for its installed package.

Virtual environment site-package folder

When you’re done working on a project, you may want to go back to using the global Python or switch to a different virtual environment. This is easy to do using the deactivate command.

Now when I go which pip3 it’s going to point to the global environment here again.

That’s all for now… till i learn more of python.

Stay tuned.