How To Manage Anaconda Environments

Anaconda provide conda command for you to do a lot of common tasks such as install / uninstall packages, create / remove isolated python environment etc. Isolated python environment is very useful when you develope Python application for different Python version. For example, if you want to run the Python app on both Python 2.7 and Python 3.6, then you need to test the app on both the two Python version. Now you can use anaconda conda command to creat two isolated python environment, one runs Python 2.7 and the other runs Python 3.6.

1. Create Isolated Python Environment In Anaconda.

Open a terminal and run conda command with below arguments to create an isolated python environment runs python version 2.7 based on anaconda libraries. The new environment name is env_py2. The installation process will take some time to download and install anaconda libraries.

$ conda create -n env_py2 anaconda python=2.7

2. Activate Isolated Python Environment In Anaconda.

When above installation process complete, run conda activate command to activate the virtual python environment. Then run python interpreter, you will find the current Python version is 2.7.

:~$ conda activate env_py2
(env_py2) :~$ python
Python 2.7.15 |Anaconda, Inc.| (default, Dec 14 2018, 19:04:19) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

3. List Isolated Anaconda Python Environment.

You can create multiple isolated python environment, if you want to see all the python environment, you can run $ conda env list command to list all isolated Python environment. The environment in the list which contains * at the beginning of the environment path is the current active Python environment.

(env_py2) :~$ conda env list
# conda environments:
#
base                     /home/zhaosong/anaconda3
env_py2               *  /home/zhaosong/anaconda3/envs/env_py2

4. Deactivate Anaconda Isolated Python Environment.

Run $ conda deactivate command to deactivate and exit current Python environment.

(env_py2):~$ conda deactivate
(base):~$ 

5. Remove Anaconda Isolated Python Environment.

$ conda remove -n env_name package_name command can remove the isolated python envrionment. But you can not remove current active Python environment, if you want to remove current active python environment, you should deactivate it first.

# --all means remove all packages from env_py2

(env_py2) :~$ conda remove -n env_py2 --all

CondaEnvironmentError: cannot remove current environment. deactivate and run conda remove again
(env_py2) :~$ conda deactivate 

(base) :~$ conda env list
# conda environments:
#
base                  *  /home/zhaosong/anaconda3
env_py2                  /home/zhaosong/anaconda3/envs/env_py2


(base) :~$ conda remove -n env_py2 --all

Remove all packages in environment /home/zhaosong/anaconda3/envs/env_py2:

Reference

  1. How To Install Python Django In Virtual Environment
READ :   How To Run Python 2 Code In Jupyter Notebook

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.