Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using requirements.txt to automatically install packages from conda channels and pip in a new conda environment

I'm trying to set a conda environment using a requirements.txt file that a coworker shared with me. My coworker uses Python in a Mac without Anaconda, and I'm using it in a Windows machine with Anaconda. The file requirements.txt was generated with the command pip freeze and looks like this:

absl-py==0.7.1
affine==2.3.0
agate==1.6.0
agate-dbf==0.2.0
agate-excel==0.2.1
agate-sql==0.5.2
...

After checking the answer of this question, I tried the following in the Anaconda terminal:

conda create --name my-env-name --file requirements.txt

Which fails with the following error message:

PackagesNotFoundError: The following packages are not available from current channels:

  - appscript==1.0.1
  - style==1.1.0
  - senticnet==1.3
  - scikits.optimization==0.3
...

My understanding is that this happens because those packages are not available in the Anaconda package installation channels, and that they should be installed instead via pip with my conda environment activated, using pip install -r requirements.txt

The problem is that this list of packages is very long, and I would like to avoid having to manually check and separating which packages are included in Anaconda channels and which should be installed via pip. Then, is there a way to tell Anaconda to create an environment by automatically recognizing the packages included in its channels, installing them, and then installing the rest using pip?

like image 969
Luise Avatar asked Mar 03 '23 04:03

Luise


1 Answers

Using requirements.txt with conda

There's no problem at all using a requirements.txt file when creating a conda environment.

In fact, you can also set additional channels at creation time:

conda create --name my-env-name --file requirements.txt --channel <NAME_OF_CHANNEL>

for example, in the case of the first package you mention, you can install it from anaconda channel. So you could run:

conda create --name my-env-name --file requirements.txt --channel default --channel anaconda

Why using default channel first? Well, just to give preference to the default one (the priority of channels is specified by the order they are listed: higher priority from left to right).

When at least some of the packages are not available using conda

Well, when no conda channel can provide any of your required packages, there are several alternatives:

  1. Install through conda those packages available in any of its channels.

  2. Install through pip the rest.

  3. Create a conda environment.yml file:

     conda env export > environment.yml
    

When you need to recreate this environment, then you can do:

conda env create --name my-env-name --file environment.yml

and it will install the packages using conda, will install pip, and then will install those packages only available with the latter one.

This approach has good and bad properties:

  • one of the good properties is that it separates those packages installed through conda from those installed using pip.
  • one of the bad properties is that it's only useful for conda, but not for pip alone.
like image 138
Nicolás Ozimica Avatar answered Mar 05 '23 15:03

Nicolás Ozimica