Published on: 24 Oct, 2021

Updated on: 24 Oct, 2021

Explore How To Create & Work With Python Virtual Environment

Here, we will discuss regarding Python virtual environment

If you are a long term or hardcore Python developer then definitely you would have worked or will work on multiple projects with different versions and packages of Python.

But it doesn't mean every time you start working on new versions or packages of Python you uninstall the existing one and then you start setting up everything from scratch. Or you start looking for a new system to install a new Python version and packages.

Obviously, you can go for a new system if your bank accounts get flooded with cash 😁 or if you have a hell lot of money that you not able to count and every morning you just swim in that like Uncle Scrooge McDuck 😁 Just kidding.

Don't worry we guys don't need to do any of the above stuff since Python has already one of the amazing features of a virtual environment that makes our job so easy and helps to set different versions and packages of Python on the same system.

So, let's get dive into the creation of virtual env and its usage using Python

What is a virtual environment?

Python virtual environment is nothing but a separate instance of Python which has its own set of dependencies Python virtual environments are to create an isolated environment for Python projects. This means that each project can have its own dependencies, regardless of what dependencies every other project has. The great thing about this is that there are no limits to the number of environments you can have since they’re just directories containing a few scripts. Plus, they can be easily created by using the virtualenv or venv command line tools.

Creation of a virtual environment

Different packages can be used to create a Python virtual environment but most commonly used ones are:

venv virtualenv conda

Virtual environment using venv

1. If you are using Python 3 then you don't need to explicitly install venv to create a virtual environment since it comes with Python 3

python -m venv virtualenvname

2. If you have both Python 2 and Python 3 installed on your system then you need to specify Python3 to create virtual environment having Python 3

python3 -m venv virtualenvname

For more info, please refer - https://docs.python.org/3/tutorial/venv.html

Virtual environment using virtualenv

1. If you are not using Python 3 then you need to explicitly install virtualenv to create a virtual environment.

2. To install virtualenv

pip install virtualenv

3. To create a virtual environment

virtualenv virtualenvname

For more info, please refer - https://docs.python-guide.org/dev/virtualenvs/#lower-level-virtualenv

Virtual environment using conda

If you are using Python for your data science projects then obviously you would have Anaconda installed on your system. And it's quite easy to create a virtual environment using anaconda

1. To create a virtual environment:

conda create -n yourenvname python=x.x anaconda

For more info, please refer - https://docs.conda.io/projects/conda/en/4.6.0/_downloads/52a95608c49671267e40c689e0bc00ca/conda-cheatsheet.pdf

Activate your virtual environment

Once we have created virtual env, we need to activate in order to use.

Activating virtual environment is pretty straight forward but yes it differs for Mac, Linux & Windows

1. Open terminal/CMD and navigate to the folder where we have created virtual environment using:

cd path_virtual_env_folder

2. Once you are in virtual environment folder

For window users execute - \venvfolder\Scripts\activate.bat

For Mac/Linux users execute - source /venvfolder/bin/activate

For conda users execute - source activate virtualenvfolder

Once you activate your virtual environment, you will see the name of your environment on terminal within ()

(virtualenv) gaurnitai at rsharmas-MacBook-Pro in ~/Desktop

Deactivate your virtual environment

Deactivating virtual environment is as simple as activation

1. Open terminal/CMD and execute

For window/Mac/Linux users - deactivate

For conda users - source deactivate

Delete your virtual environment

Deleting virtual environment is super easy. You have to simply delete the virtual environment folder

1. Open terminal/CMD and navigate to the folder where we have created virtual environment using:

cd path_virtual_env_folder

2. Once you are in virtual environment folder simple execute

For window/Mac/Linux users - rm -rf virtualenvname

For conda users - conda remove -n virtualenvname -all

Structure of virtual environment folder

A virtual environment folder mainly consists of couple of subfolders & files

Here, is the folder structure details:

bin: files that interact with the virtual environment

include: C headers that compile the Python packages

lib: a copy of the Python version along with a site-packages folder where each dependency is installed

Share with your love's one!