Understanding conda and pip
Understanding conda and pip
Although some of the functionality of conda and pip overlap (namely, the ability to install Python packages), they were designed and should be used for different purposes. Pip is the Python Packaging Authority’s recommended tool for installing packages from the Python Package Index, PyPI. Conda, on the other hand, is a cross-platform package and environment manager that installs and manages packages from the Anaconda public repository as well as from Anaconda.org.Other key differences between conda and pip include:
conda | pip | |
---|---|---|
Package distribution format | Binaries | Wheels or source |
Requires compilers? | No | Yes |
Package types | Any (Python, R, C++, and so on) | Python only |
Environment creation? | Yes, built in | No, requires virtualenv or venv |
Dependency resolution? | Yes | No |
Package sources | Anaconda repo, Anaconda.org | PyPI |
Creating an environment.yml
file manually
To create a stable environment that includes pip packages, Anaconda recommends writing an environment.yml
file and then building an environment from that file. Although this method is more time consuming to set up, it offers several advantages:
- Control over package build order, versions, and channels
- Straightforward environment updates
- Better reproducibility and shareability via a
.yml
file
Writing an environment.yml
file
The following is an example environment.yml
file. When writing the file, be sure to add pip and its dependencies last, since conda builds environments in the order listed.
Creating an environment from an environment.yml
file
To create an environment from an environment.yml
file, run the following command from the directory containing the file:
Updating an environment with an environment.yml
file
If you ever need to add packages to your environment, make changes to package versions, or remove packages, update the environment.yml
file, then rebuild the environment by running the following command from the directory containing the file:
The
--prune
option removes any orphaned packages from the environment. A package is considered orphaned if it meets both these criteria:- It wasn’t explicitly installed by the user.
- It isn’t a dependency for any currently installed packages.
Using pip install
in a conda environment
Because of conda’s lack of awareness of environment updates made by pip, using pip in your environment must be the last action that will be performed when building the environment.
Do not run
pip install
in your base environment. Create a separate conda environment to isolate changes.- Activate your target environment.
-
Install the conda packages first by running the following command:
Both Anaconda and Miniconda include pip, but you must install it in your working environment to execute pip commands.
-
To install a PyPI package, run the following command: