1 Jul 2020

Python Virtual environment Cheat sheet



What is virtual environment?

Virtual environment is a space or tool which helps to keep libraries or packages for different projects separate by creating virtual environments.
In this post I will list down all popular commands to work with python virtual environment with Anaconda.
But before that let’s understand why python virtual environment is required.

Why do we need virtual environment?

Let’s assume you are working on a web application where you are using Django 1.11 and now you want to start another project where you need Django 2.0.
That means you have to use both Django 1.11 and Django 2.0 at the same time.
In this kind of situations virtual environment can be really useful to maintain libraries of both the projects.
Now let me list down popular commands for python virtual environment.
Note: All commands listed below for python with Anaconda. If you are not using Anaconda then below commands may not work.


Python Virtual environment Commands

·       Create virtual environment with all basic packages of anaconda (like pandas, numpyjupyter etc.). In below command you need to specify exact Python version (2.7, 3.6, 3.6.3, 3.7 etc.) you want to work with.
conda create -n ENV_NAME python=3.6.3 anaconda
·       Create completely isolated (no basic packages will be installed in it) python virtual environment

conda create -n ENV_NAME python=3.6.3

·       Remove specific virtual environment
conda env remove -n ENV_NAME
·       Print list of all python virtual environments you have created
conda env list

Print in which environment you are currently working

In Python script write below code to print in which Python environment you are currently working
import sys
print sys.prefix
You can also do that through command line by below command.
pip -V

Please let me know if want me to add any other commands for Python virtual environment. See you in comment section below.