Installing pip packages
Most of the popular packages from the PyPI repository are available in either Anaconda’s public repository, Anaconda.org, or conda-forge. However, you might need to use pip if a package or specific version is not available through conda channels.
Installing packages using pip modifies your conda environment, but conda isn’t aware of these modifications. As a result, when conda later attempts to modify the environment, there’s a high probability that dependency conflicts will arise between the conda-tracked packages and the untracked pip packages, which can lead to a broken environment.
Follow the guidance on this page when using pip in a conda environment to avoid dependency conflicts and broken environments.
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.
The official conda documentation includes more information on creating environment files manually, as well as package match specifications.
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:
See the official conda documentation for details on creating an environment from a .yml 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.
To build a conda environment that contains PyPI packages at the command line, complete the following steps:
-
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:
If you need to install additional conda packages after installing pip packages, create a new environment and reinstall the packages following the process outlined above.
For more information, see the official conda documentation on using pip with conda.
Was this page helpful?