Skip to content

Instantly share code, notes, and snippets.

@luiscape
Created January 16, 2017 14:36
Show Gist options
  • Save luiscape/19d2d73a8c7b59411a2fb73a697f5ed4 to your computer and use it in GitHub Desktop.
Save luiscape/19d2d73a8c7b59411a2fb73a697f5ed4 to your computer and use it in GitHub Desktop.
Install Python dependency packages from requirements.txt using conda.
#
# Original solution via StackOverflow:
# http://stackoverflow.com/questions/35802939/install-only-available-packages-using-conda-install-yes-file-requirements-t
#
#
# Install via `conda` directly.
# This will fail to install all
# dependencies. If one fails,
# all dependencies will fail to install.
#
conda install --yes --file requirements.txt
#
# To go around issue above, one can
# iterate over all lines in the
# requirements.txt file.
#
while read requirement; do conda install --yes $requirement; done < requirements.txt
@ShixiangWang
Copy link

This should delete comment lines firstly.

@IbnNafis007
Copy link

I hope they fix this in the conda command, if this is really the case (maybe it's a problem with some packages' dependencies specification rather than conda), because conda says:
conda install --help (...) --file FILE Read package versions from the given file. Repeated file specifications can be passed (e.g. --file=file1 --file=file2). --no-deps Do not install, update, remove, or change dependencies. This WILL lead to broken environments and inconsistent behavior. Use at your own risk. --only-deps Only install dependencies.

This means, it should acually handle the deps.

BTW, the second part is a plain bash loop. In Windows it's done like this:

for /f %i in (requirements.txt) do conda install --yes %i - when run manually
for /f %%i in (requirements.txt) do conda install --yes %%i - when inside a BAT script.

Great

@SpyderRivera
Copy link

conda install --file requirements.txt

@oskar-j
Copy link

oskar-j commented Feb 26, 2019

Yes @SpyderRivera seems like they've added this recently

@pradyumnasagar
Copy link

what to do if the packages in requirement.txt are from different channel?

@abhatnag
Copy link

abhatnag commented Apr 2, 2019

@pradyumnasagar append to the list of channels that conda will search for. For instance, if you want to use conda-forge:
conda config --append channels conda-forge

Copy link

ghost commented Jun 27, 2019

Thanks luiscape, for the tip on solving the requirements.txt with conda.

@FhyTan
Copy link

FhyTan commented Aug 8, 2019

Thanks, an elegant way to make pip list compatible in Conda environment.

@hmwasita
Copy link

hmwasita commented Nov 6, 2020

I hope they fix this in the conda command, if this is really the case (maybe it's a problem with some packages' dependencies specification rather than conda), because conda says:
conda install --help (...) --file FILE Read package versions from the given file. Repeated file specifications can be passed (e.g. --file=file1 --file=file2). --no-deps Do not install, update, remove, or change dependencies. This WILL lead to broken environments and inconsistent behavior. Use at your own risk. --only-deps Only install dependencies.

This means, it should acually handle the deps.

BTW, the second part is a plain bash loop. In Windows it's done like this:

for /f %i in (requirements.txt) do conda install --yes %i - when run manually
for /f %%i in (requirements.txt) do conda install --yes %%i - when inside a BAT script.

Thanks, It worked for me

@Fahad021
Copy link

conda install --file requirements.txt

does not work

@Fahad021
Copy link

I hope they fix this in the conda command, if this is really the case (maybe it's a problem with some packages' dependencies specification rather than conda), because conda says:
conda install --help (...) --file FILE Read package versions from the given file. Repeated file specifications can be passed (e.g. --file=file1 --file=file2). --no-deps Do not install, update, remove, or change dependencies. This WILL lead to broken environments and inconsistent behavior. Use at your own risk. --only-deps Only install dependencies.
This means, it should acually handle the deps.
BTW, the second part is a plain bash loop. In Windows it's done like this:
for /f %i in (requirements.txt) do conda install --yes %i - when run manually
for /f %%i in (requirements.txt) do conda install --yes %%i - when inside a BAT script.

Thanks, It worked for me

worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment