32

I am installing a list of packages with pip-python using the command

pip install -r requirements.txt

sometimes it fails installing packages for whatever reason. Is it possible to have it continue the the next package even with these failures?

JiminyCricket
  • 7,050
  • 7
  • 42
  • 59

4 Answers4

40

I have the same problem. continuing on the line of @Greg Haskins, maybe this bash one-liner is more succinct:

cat requirements.txt | while read PACKAGE; do pip install "$PACKAGE"; done

# TODO: extend to make the script print a list of failed installs,
# so we can retry them.

(for the non-shellscripters: it calls pip install for each of the listed packages)

the same note on the dependancies failure applies of course here!

caesarsol
  • 2,010
  • 1
  • 20
  • 21
  • 1
    Is TODO section done? Why does it works slower than `pip install -r requiremnets.txt`? – alper Apr 25 '21 at 13:41
  • 1
    the TODO was never implemented. it's slower because it runs pip for every package, and not one single time for all packages, so every startup time gets accumulated. note that this solution is not "smart", but it works for the quick&dirty moments. I would recommend to fix the failing dependency! – caesarsol Apr 26 '21 at 16:59
  • This worked well, thank you – You_Dont_Know_Me Nov 04 '21 at 21:24
19

You could write a little wrapper script to call pip iteratively, something like:

#!/usr/bin/env python
"""
pipreqs.py: run ``pip install`` iteratively over a requirements file.
"""
def main(argv):
    try:
        filename = argv.pop(0)
    except IndexError:
        print("usage: pipreqs.py REQ_FILE [PIP_ARGS]")
    else:
        import pip
        retcode = 0
        with open(filename, 'r') as f:
            for line in f:
                pipcode = pip.main(['install', line.strip()] + argv)
                retcode = retcode or pipcode
        return retcode
if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv[1:]))

which you could call like pipreqs.py requirements.txt --some --other --pip --args.

Note that this only applies the "continue despite failure" motto one level deep---if pip can't install a sub-requirement of something listed, then of course the parent requirement will still fail.

Greg Haskins
  • 6,714
  • 2
  • 27
  • 22
  • Thanks, this seems to work. There is a annoying side-effect though: the log-messages from pip seem to duplicate (and triplicate, etc) the more packages are listed in the requirements file. It is probably a logger instantiation problem. – blueFast Sep 03 '14 at 22:46
  • In case of `AttributeError: 'module' object has no attribute 'main'`, see https://stackoverflow.com/a/49853826/1878788 – bli May 31 '18 at 08:56
11

On Windows command prompt/ cmd:

# For each package,p, in requirements.txt, pip install package
FOR /F %p IN (requirements.txt) DO pip install %p
Dave
  • 424
  • 3
  • 14
0

Modify the pip requirement file, add the pip command like pip install to the beginning of each line. E.g., using vim:

  1. enter visual-block mode (ctrl-v),
  2. move the cursor to the beginning column
  3. shift+i
  4. type or paste pip install
  5. press esc
Good Pen
  • 625
  • 6
  • 10