> ## Documentation Index
> Fetch the complete documentation index at: https://anaconda.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Installing pip packages

Most of the popular packages from the PyPI repository (Any storage location from which software or software assets, like packages, can be retrieved and installed on a local computer.) 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 (A location (URL or file path) in a repository where conda looks for packages.).

> [!TIP]
>
> Before reaching for pip, check whether the package is available in the [conda-pypi](https://anaconda.org/channels/conda-pypi) channel. The conda-pypi channel indexes pure Python wheels from PyPI and lets you install them with `conda install`, keeping your entire environment under conda's dependency management. For setup instructions, see [Installing packages from PyPI with conda](https://docs.conda.io/projects/conda/en/stable/user-guide/tasks/install-packages-from-pypi.html) in the official conda documentation.

Installing packages using pip modifies your conda environment (An isolated folder containing specific conda packages and dependencies that won't interfere with other projects. Each environment maintains its own versions of libraries and packages, so changes in one environment don't affect others.), 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.

> [accordion] 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 |

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.

```sh theme={null}
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](https://anaconda.com/docs/getting-started/working-with-conda/environments.md#creating-an-environment-from-a-yaml-file) from an `environment.yml` file, run the following command from the directory containing the file:

```sh theme={null}
conda 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:

```sh theme={null}
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.

> [!WARNING]
>
> Do not run `pip install` in your base environment. Create a [separate conda environment](https://anaconda.com/docs/getting-started/working-with-conda/environments.md) 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:

   ```sh theme={null}
   conda install <PACKAGE> <PACKAGE> pip
   ```

   Comments

   Replace \<PACKAGE> with the conda packages you need to install, separated by a space.\\

   List the `pip` package last.

   > [!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:

   ```sh theme={null}
   pip install <PACKAGE>
   ```

   Comments

   Replace \<PACKAGE> with the name of the PyPI package you need to install.

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