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.

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++, etc.)

Python only

Environment creation?

Yes, built in

No, requires virtualenv or venv

Dependency resolution?

Yes

No

Package sources

Anaconda repo, Anaconda.org

PyPI

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.

name: myenv # This will become the name of your environment
dependencies: # The list of packages to include in your environment
    - python=3.12 # You can specify versions
    - bokeh>=2.4.2
    - flask
    - pip # Install pip in your environment
    - pip: # Include pip dependencies last
        - Flask-Testing

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:

conda env create --file environment.yml

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:

conda env update --file environment.yml --prune

Note

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.

Caution

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:

  1. Activate your target environment.

  2. Install the conda packages first by running the following command:

    # Replace <PACKAGE> with the conda packages you need to install, separated by a space
    # List the pip package last
    conda install <PACKAGE> <PACKAGE> pip
    

    Note

    Both Anaconda and Miniconda include pip, but you must install it in your working environment to execute pip commands.

  3. To install a PyPI package, run the following command:

    # Replace <PACKAGE> with the name of the PyPI package you need to install
    pip install <PACKAGE>
    

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.