mskel
mskel

Reputation: 872

Installing dependencies for a pip-only package through conda

Sometimes I need to install a pip-only package into a conda environment. If I install the package using pip install, then all the dependencies for that package are installed using pip, even if they are available to conda.

I would like to install as many packages as possible through conda, so currently I use a hack to get the list of package dependencies through pip, search for all of them on conda, conda install the ones that are found, and then go through with the pip install.

Am I right to prefer installing dependencies through conda rather than pip? And if so, can anyone think of a more elegant way to solve this problem?

Upvotes: 12

Views: 2982

Answers (2)

Stuart Berg
Stuart Berg

Reputation: 18141

Am I right to prefer installing dependencies through conda rather than pip?

Yes.

Anaconda has a blog post that discusses best practices when you have no choice but to combine conda and pip. Here's a list of guidelines from that blog post:

Best Practices Checklist

  • Use pip only after conda

    • install as many requirements as possible with conda, then use pip
    • pip should be run with --upgrade-strategy only-if-needed (the default)
    • Do not use pip with the --user argument, avoid all “users” installs
  • Use conda environments for isolation

    • create a conda environment to isolate any changes pip makes
    • environments take up little space thanks to hard links
    • care should be taken to avoid running pip in the “root” [base] environment
  • Recreate the environment if changes are needed

    • once pip has been used conda will be unaware of the changes
    • to install additional conda packages it is best to recreate the environment
  • Store conda and pip requirements in text files

    • package requirements can be passed to conda via the --file argument
    • pip accepts a list of Python packages with -r or --requirements
    • conda env will export or create environments based on a file with conda and pip requirements

Upvotes: 3

cel
cel

Reputation: 31339

pip and conda are two separate package managers. Only in very rare cases package managers actually work together. In practical applications conda and pip usually do not.

In reality, mixing conda and pip packages is usually unavoidable. This often leads to a messy package management, as you describe.

In my opinion, the best and currently only proper way to solve this problem is to create a conda package for all (pypi-)packages and dependencies you want to use in your conda environments.

conda-forge is a community effort that offers an easy way to contribute your own package to the conda infrastructure. You may want to check out if your package is already available, and if not if contributing is an option for you.

Upvotes: 5

Related Questions