Install only available packages using "conda install --yes --file requirements.txt" without errors

When installing packages into require.txt using Conda with the following command

conda install --yes --file requirements.txt 

If the package in the require.txt file is not available, the error message "No package" is displayed, for example, as shown below:

Using the Anaconda Cloud API https://api.anaconda.org

Fetching package metadata: ....

Error: no packets were found in the current linux-64 channels: nimfa == 1.2.3

You can find this package on anaconda.org using

 anaconda search -t conda nimfa ==1.2.3 

Instead of throwing an error, is it possible to change this behavior so that it installs all available packages in needs.txt and generates a warning for those that are not available?

I would like this because, a nimfa package that says the error is not available can be installed pip. Therefore, if I can change the behavior of conda install --yes --file requirements.txt to just give a warning for inaccessible packages, I can run the pip install -r requirments.txt command in .travis.yml so that TravisCI tries to install it from any place where it is available.

+64
python pip anaconda
Mar 04 '16 at 17:58
source share
4 answers

In the end, I just went over the lines of the file

$ while read requirement; do conda install --yes $requirement; done < requirements.txt

Change: If you want to install the package using pip, if it is not available through conda, try:

$ while read requirement; do conda install --yes $requirement || pip install $requirement; done < requirements.txt

Change: If you use Windows (credit goes to @Clay ):

$ FOR/F "delims=~" %f in (requirements.txt) DO conda install --yes "%f" || pip install "%f"

+123
Jul 27 '16 at 9:56 on
source share

You can do it as indicated in this

Export to .yml file

 conda env export > freeze.yml 

Play:

 conda env create -f freeze.yml 
+8
Aug 16 '17 at 13:05
source share

For those looking, I used this as @TillHoffmann's solution for fish shells:

 $ while read requirement; conda install --yes $requirement; end < requirements.txt 

As well as

 $ while read requirement; conda install --yes $requirement;or pip install $requirement; end < requirements.txt 
+1
Jul 18 '18 at 22:46
source share

The Pbms answer here is the right way to do this, assuming you have an existing copy environment. Conda is fully capable of installing both Conda and pip packages as specified in environment.yml . I wanted to document the whole process in more detail. Note that I use folder-based environments, so I added --prefix [path to environment folder] to most commands.

Suppose you installed the environment for an existing project in a folder named env in the current folder, for example:

 conda create --prefix ./env 

You must generate environment.yml for this project environment, for example:

 conda env export --prefix ./env > environment.yml 

You would create a new environment in some other folder by copying environment.yml there and then running it from there:

 conda env create --prefix ./env --file environment.yml 

You will get an already existing environment corresponding to environment.yml , once again copying environment.yml there and then running it from there:

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

If the environment in question is active, you can check the status of its packages as follows:

 conda list 

This is a shortened version of what this command can print (note that pip packages are marked with pypi ):

 # Name Version Build Channel pip 19.2.2 py37_0 python 3.7.4 h5263a28_0 numpy 1.16.4 py37h19fb1c0_0 pandas 0.25.1 py37ha925a31_0 pyodbc 4.0.27 py37ha925a31_0 ibm-db 3.0.1 pypi_0 pypi ibm-db-sa 0.3.5 pypi_0 pypi 

Finally, this is a shortened version of what environment.yml might look like (note that pip packages are listed in their own category):

 dependencies: - pip=19.2.2=py37_0 - python=3.7.4=h5263a28_0 - numpy=1.16.4=py37h19fb1c0_0 - pandas=0.25.1=py37ha925a31_0 - pyodbc=4.0.27=py37ha925a31_0 - pip: - ibm-db==3.0.1 - ibm-db-sa==0.3.5 

Keep in mind that sharing Conda and pip can cause some heartburn, as they can unknowingly destroy dependencies on each other. You must first install all your Conda packages, and then all your pip packages, and not interleave them. If your environment is broken, the official recommendation is to delete and recreate it (from your environment.yml file). See this guide for more information:

https://www.anaconda.com/using-pip-in-a-conda-environment/

0
Aug 30 '19 at 18:24
source share



All Articles