How to Manage Python Dependencies with Conda
Conda is a package, dependency, and environment management tool for Anaconda Python, which is widely used in the scientific community, especially on the Windows platform where the installation of binary extensions can be difficult.
Conda helps manage Python dependencies in two primary ways:
- Allows the creation of environments that isolate each project, thereby preventing dependency conflicts between projects.
- Provides identification of dependency conflicts at time of package installation, thereby preventing conflicts within projects/environments.
How Does Conda Compare to Pip, Virtualenv, Venv & Pyenv
Conda provides many of the features found in pip, virtualenv, venv and pyenv. However it is a completely separate tool that will manage Python dependencies differently, and only works in Conda environments.
Conda analyzes each package for compatible dependencies, and how to install them without conflict. If there is a conflict, Conda will let you know that the installation cannot be completed. By comparison, Pip installs all package dependencies regardless of whether they conflict with other packages already installed. To avoid dependency conflicts, use tools such as virtualenv, venv or pyenv to create isolated Anaconda environments.
For information about the use of pip in conda environments, refer to this Quickread post. How to Add Packages in Anaconda Python: Conda Vs. Pip.
Our ActiveState Platform takes care of dependencies for you. It also helps eliminate “works on my machine” issues, simplifies the Readme and lets you get to the fun coding parts faster. Use it for Python, Perl and Tcl runtimes for Linux, Windows and MacOS. Get started for free.
Conda is a command-line tool and is included in the Anaconda distribution. It can be run from the Anaconda Prompt in Windows or in a Linux terminal. It is usually quicker and more practical to use Conda than the GUI-based Anaconda Navigator, which can also be used for dependency, and environment management.
Before working with Conda, it’s always good practice to ensure that the latest versions of Conda and Anaconda are installed. Open an Anaconda Prompt or Linux terminal and enter:
$ conda update conda --all $ conda update anaconda
Conda can be used to create, export, list, remove, and update environments that have different Python versions and different packages installed in them.
To create a new Conda Python environment named <env_name> and install python 3.8, open an Anaconda Prompt or terminal and enter:
$ conda create --name <env_name> python=3.8
To create an environment with a specific package:
$ conda create -n <env_name> <packagename>
If conda encounters a dependency conflict during the installation of a package it will flag it to the user. For example:
$ conda install -c menpo opencv=2.4.11 Fetching package metadata ............. Solving package specifications: . UnsatisfiableError: The following specifications were found to be in conflict: opencv 2.4.11* -> numpy 1.9* -> python 2.6* -> openssl 1.0.1* python 3.6* Use "conda info " to see the dependencies for each package.
As suggested in the error message, you can use the command conda info to manually view each package’s dependencies, and hopefully find versions that will a) be compatible with each other, and b) work with your project.
How to Activate an Environment with Conda
To ‘activate an environment’ is to switch from one environment to another. It’s a best practice to create a new environment for each project you work on, so switching environments can be thought of as switching between projects.
To activate a new environment that has Python 3.8, and switch to it:
$ source activate <env_name>
$ activate <env_name>
To install a package in the currently activated environment:
$ conda install <packagename>
How to Determine the Current Environment with Conda
The current or active environment is shown in parentheses () or brackets  at the beginning of the Anaconda Prompt or terminal:
Recommendations for Avoiding Dependency Conflicts with Conda
There are two simple rules to follow:
- Always create a new environment for each project
- Install all the packages that you need in the new environment at the same time. Installing packages one at a time can lead to dependency conflicts.
To create an environment with a specific version of Python and multiple packages including a package with a specific version:
$ conda create -n <env_name> python=<version#> <packagename> <packagename> <packagename>=<version#>
Alternatively, you can use conda to install all the packages in a requirements.txt file. You can save a requirements.txt file from an existing environment, or manually create a new requirements.txt for a different environment.
To create a conda requirements.txt file from an existing environment:
- Activate your project environment. See section above entitled “How to Activate an Environment with Conda” for details.
- List the packages in use in the activated environment:
$ conda list
- Save the package info to a requirements.txt.file
$ conda list -e > requirements.txt
Conda requirements.txt example:
# This file may be used to create an environment using: # $ conda create --name <env> --file <this file> # platform: win-32 ca-certificates=2020.1.1=0 certifi=2020.4.5.1=py38_0 openssl=1.1.1f=he774522_0 pip=20.0.2=py38_1 ... ... wheel=0.34.2=py38_0
- Install the requirements.txt file into the new project environment:
$ conda install -n <env_name> requirements.txt
For more information on working with conda, refer to: How to Add Packages in Anaconda Python
Frequently Asked Questions
A requirements.txt file is typically used to simplify the installation of multiple packages since you can install them all with a single command. To conda install requirements.txt, run the following command:
conda install -n <env_name> requirements.txt
All the packages listed in your requirements.txt will be installed into the specified environment.
For more information on working with conda environments, see How To Add Packages In Anaconda Python.
Or learn more about ActivePython as an Anaconda alternative, which lets you install requirements.txt and automate dependency resolution.
If you already have a conda environment and want to generate a requirements.txt file containing all the packages in that environment, you can run the following command:
conda list -e > requirements.txt
The resulting requirements.txt.file will contain a list of all package names and versions.
For more information on working with conda, see How To Add Packages In Anaconda Python.
A requirements.txt file is typically used to simplify the installation of multiple packages since you can install them all with a single command. To try it out, just:
- Create a requirements.txt file
- Enter the name of the package(s) you want to install, and optionally include the version (if you don’t include the version number, pip will install the latest)
- Run either:
pip install requirements.txt
conda install -n <env_name> requirements.txtif you’re working with Anaconda
Running the command
pip install requirements.txt will install the dependencies listed in your requirements.txt file. This can be a significant time saving rather than installing each package one at a time, and ensures that the correct version of each package is installed.
For more information on working with pip on Windows, see Python Package Installation On Windows.