Conda Virtual environment Cheatsheet

conda virtual environment

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. 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 the below commands may not work.

Python Virtual environment Commands

 Create virtual environment with all basic packages

In below command, you need to specify the exact Python version (2.7, 3.6, 3.6.3, 3.7 etc.) you want to work with. It will create virtual environment with all basic packages of anaconda (like pandas, numpy, etc.). 

conda create -n ENV_NAME python=3.6.3 anaconda

Create an isolated Conda environment

Create a completely isolated python virtual environment. No basic packages will be installed in it.

conda create -n ENV_NAME python=3.6.3

Conda Remove environment

Run the below command to remove a specific virtual environment through cmd.

conda env remove -n ENV_NAME

Conda list all Environments

Below command, to print all python virtual environments you have created.

conda env list

Conda Activate Virtual Environment

Below command to activate any specific virtual environment.

activate ENV_NAME

Print current environment

There are two ways to see the environment in which you are currently working in. 1. using python, 2. using command line

Also Read:  Use Opencv with GPU with just 2 lines of code

1. Using Python

In Python script write below code to print in which Python environment you are currently working

import sys
print (sys.prefix)

2. Using Command Line

You can also do that through command line by the below command.

pip -V

Get path of Conda environment variable

Below command to show the folder directory path of the current environment

echo %CONDA_PREFIX%

Conda list all packages in environment

By running below command you will get list of all python libraries installed inside current environment

conda list

Install Jupyter notebook in Conda environment

If you want to use Jupyter notebook inside any conda environment, you need to install it. To do that run below commands:

activate ENV_NAME
pip install --user ipykernel
python -m ipykernel install --user --name=ENV_NAME

Conclusion

In this post I tried to list all commands for virtual environment in conda which we mostly used day to day basis.

Please let me know in the comment section below if I missed mentioning any useful commands.

Leave a Comment

Your email address will not be published. Required fields are marked *