Manage your Python Virtual Environment with Conda | by June Tao Ching | Towards Data Science

Although Python 2 is officially deprecated (Python 3, your time is now!), I believe some of us still have to maintain existing Python 2 projects before fully port those projects to Python 3.
In this article, I will show you how to manage your Python virtual environment by using Conda. Conda is a package and environment management system, it allows us to create and switch environments easily on our local machine.
I will be using the iTerm terminal in macOS. Windows user will have to use Anaconda Prompt if you never add the Conda environment path to your windows environment variable.
% conda info
To show information about current Conda install.
(base) username % conda env list# conda environments:#base                  *  /Users/username/opt/anaconda3py2                      /Users/username/opt/anaconda3/envs/py2
We can use conda env list to list all existing Python environments. The * will point to the current active environment. base is always the default active environment when you open your terminal. py2 is my another virtual environment for my Python 2 projects.
(base) username % conda create --name project-env python=3.7
Let’s say you want to create a virtual environment for your new project, we can use conda create to create a new environment named project-env. We can also specify the Python version when creating the environment.
After creating your new environment, you can run conda env list to check your new environment.
(base) username % conda env list# conda environments:#base                  *  /Users/username/opt/anaconda3project-env              /Users/username/opt/anaconda3/envs/project-envpy2                      /Users/username/opt/anaconda3/envs/py2
The environments created by Conda is always located in /Users/.../anaconda3/envs/. You may change the default location by using the following command but it is not encouraged. Conda can no longer find your environment by your environment name, you will have to specify the environment’s full path to activate it every time.
(base) username % conda create --prefix /path/project-env
As you can see, you have successfully create your new Python environment. Now we have to activate and use it.
(base) username % conda activate project-env(project-env) username %
The (project-env) indicates the current active environment.
You can use conda list to display all packages in this environment.
(project-env) username ~ % conda list# packages in environment at /Users/username/opt/anaconda3/envs/project-env:## Name                    Version                   Build  Channelca-certificates           2020.1.1                      0pip                       20.0.2                   py37_1python                    3.7.6                h359304d_2readline                  7.0                  h1de35cc_5setuptools                45.1.0                   py37_0......
conda list also supports revision history. It is an incredibly useful feature to have to manage your package in the environment.
(project-env) username ~ % conda list --revision2020-02-03 00:01:32  (rev 0)       # new environment    +ca-certificates-2020.1.1    +pip-20.0.2    +python-3.7.6    +readline-7.0    +setuptools-45.1.0...2020-02-03 00:24:55  (rev 1)       # installed numpy    +blas-1.0    +intel-openmp-2019.4    +libgfortran-3.0.1    +mkl-2019.4    +mkl-service-2.3.0    +mkl_fft-1.0.15    +mkl_random-1.1.0    +numpy-1.18.1    +numpy-base-1.18.1    +six-1.14.02020-02-03 22:14:24  (rev 2)      # installed configparser    +configparser-3.5.0
Imagine you accidentally screw up your dependencies just because you update/install a new package. Revision history allows you to rollback your environment to the previous version just like that.
(project-env) username ~ % conda install --revision 1## Package Plan ##environment location: /Users/username/opt/anaconda3/envs/project-envadded / updated specs:    - configparser    - numpy    - python=3.7The following packages will be REMOVED:configparser-3.5.0-py37_0
configparser has been removed successfully. You can use conda list — revision to check again.
(project-env) username ~ % conda list --revision2020-02-03 00:01:32  (rev 0)       # new environment    +ca-certificates-2020.1.1    +pip-20.0.2    +python-3.7.6    +readline-7.0    +setuptools-45.1.0...2020-02-03 00:24:55  (rev 1)       # installed numpy    +blas-1.0    +intel-openmp-2019.4    +libgfortran-3.0.1    +mkl-2019.4    +mkl-service-2.3.0    +mkl_fft-1.0.15    +mkl_random-1.1.0    +numpy-1.18.1    +numpy-base-1.18.1    +six-1.14.02020-02-03 22:14:24  (rev 2)     # installed configparser    +configparser-3.5.02020-02-03 22:17:02  (rev 3)     # reverted to rev 1    -configparser-3.5.0
You can always use conda activate or conda deactivate to switch between your environments. You can directly activate the environment you wish to use by using conda activate.
(project-env) username ~ % conda activate base(base) username ~ %
conda deactivate will deactivate your current active environment and change to the default environment which is the base environment.
(project-env) username ~ % conda deactivate(base) username ~ %
While you are done with this environment and wish to remove it. You can use conda env remove to remove the environment.
(base) username ~ % conda env list# conda environments:#base                  *  /Users/username/opt/anaconda3project-env              /Users/username/opt/anaconda3/envs/project-envpy2                      /Users/username/opt/anaconda3/envs/py2
Same thing as create, you have to specify the name of the environment you wish to remove by using --name.
(base) username ~ % conda env remove --name project-envRemove all packages in environment /Users/junetao/opt/anaconda3/envs/project-env:
The project-env has been successfully removed from your environments.
(base) username ~ % conda env list# conda environments:#base                  *  /Users/junetao/opt/anaconda3py2                      /Users/junetao/opt/anaconda3/envs/py2
If you are a Visual Studio Code fans like me, you can change your Python environment that you created without any command line.
Click the Python 3.7.4 at the bottom left corner of VS Code.
Select the preferred environment.
Done!
Now you have learnt how to manage your Python environment with Conda. Do drop me a comment if I made any mistake or typo. Cheers!
If you enjoyed reading this piece, you might also enjoy these:
towardsdatascience.com
towardsdatascience.com
You can find links to my other works on Medium and follow me here. Thanks for reading!