=======================
Installing pip packages
=======================

Most of the popular packages from the PyPI repository are available in either `Anaconda's public repository <https://repo.anaconda.com/pkgs/>`_, `Anaconda.org <https://anaconda.org/>`_, or `conda-forge <https://conda-forge.org/>`_. 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.

.. dropdown:: Understanding conda and pip
    :animate: fade-in  
    :color: primary  

    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:

    .. csv-table::
        :file: ../../csv/pip-vs-conda.csv
        :header-rows: 1

Follow the guidance on this page when using pip in a conda environment to avoid dependency conflicts and broken environments.

.. _manual-file-creation:

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.

.. code-block:: none

    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 <https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#create-env-file-manually>`_, as well as `package match specifications <https://docs.conda.io/projects/conda/en/latest/user-guide/concepts/pkg-specs.html#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:

.. code-block:: none

    conda env create --file environment.yml

See the official conda documentation for details on `creating an environment from a .yml file <https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#create-env-from-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:

.. code-block:: none

    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 :doc:`separate conda environment <../environments>` 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:

    .. code-block:: none

        # 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.

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

    .. code-block:: none

        # 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 <https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#pip-in-env>`_.
