In this introduction to Anaconda Environments, you will learn how to use, create, and manage python environments using the Anaconda distribution.
What is Anaconda?
Anaconda is a Python distribution for data science and machine learning.
Why Use Virtual Environments?
The main purpose of Python virtual environments is to create isolated environments where each project is independent from the other, using its own dependencies.
For example, one project could use Python 2.7 and the other Python 3.6. You could also use different versions of the same package in different projects.
Check if Conda is Installed
Open the Terminal or Command line and run the
conda -V command.
$ conda --version #or $ conda -V
If nothing appears. You will need to install Anaconda.
How to Install Anaconda
The first step is to install Anaconda if you have not done yet. Read this guide to show you how to Install Python With Anaconda [On Windows].
Check Installed Packages
The Anaconda distribution install packages automatically. To check what was installed and which version use the following command in the command prompt.
$ conda list
Get a List of the Environments
To get a list of all available environments on your machine use:
$ conda env list
The other way that you can get a list of your Anaconda Environments is through the Anaconda Navigator App.
Create a New Environment
To create a new python environment use the following command:
$ conda create --name environment-name
Create an Environment Using a Specific Version of Python
Some packages have not been updated to Python 3. If you want to use them, you need to create an environment using Python 2.7.
$ conda create -n <environment-name> python=2.7.0
Remove an Environment
You can remove an environment using the following command:
$ conda env remove --name <environment-name> # or $ conda env remove -n <environment-name>
Again, you could also remove it from the Anaconda Navigator UI.
Activate an Environment
Before you can use an environment, you need to activate it. You can activate your environment using:
$ conda activate <environment-name>
Deactivate an Environment
If you want to deactivate the environment.
$ conda deactivate
Clone an Environment
You can create a clone of an existing environment if you want to apply minor changes to the environments. This one clones the root/
$ conda create --name <environment-name> --clone base
Check Installed Packages in Environment
To check packages in an existing environment, use the following command:
$ conda list --name <environment-name>
You can also look at a specific packages.
$ conda list --name <environment-name> 'pandas|scikit-learn'
Search for a Package
$ conda search matplot
Remove a Package from Specific Environment
$ conda remove -n <environment-name> pandas
Add Package in Specific Environment
$ conda install -n <environment-name> pandas=0.25.0
Set Environment Variables
To set environment variables in Anaconda, use:
$ conda env config vars set <VARIABLE_NAME>=filename.py
To check which variables exists in the environment.
$ conda env config vars list
To remove (or unset) environment variables.
$ conda env config vars unset <VARIABLE_NAME> -n <ENVIRONMENT_NAME>
Create an Anaconda Environment from a YAML file
It is possible to create a conda environment based on a pre-defined configuration file.
This solution is faster and simpler when you have a lot of packages to install for a project.
The module requirements for the conda environment can be written into a YAML file (
.yml) to be installed upon creation.
new-environment.yaml file. Add the dependencies that you need for your project.
name: new-environment channels: - conda-forge dependencies: - python=3.8 - pandas - matplotlib - scikit-learn - numpy - pip
Now, create the environment.
From your Terminal, type:
$ conda env create -f new-environment.yaml
All the packages will get installed.
Then, activate the environment.
$ conda activate new-environment
Python environments are critical to avoid conflict of module versions between your projects. Even if this seems overly complicated, as you progress in Python you will start seeing how important this actually is.
Better start learning it.
This is it for our introduction of Anaconda Environments.