Pip Install -r continue past installs that fail

2019-03-08 11:18发布

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?

3条回答
看我几分像从前
2楼-- · 2019-03-08 11:37

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.

查看更多
The star\"
3楼-- · 2019-03-08 11: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!

查看更多
何必那么认真
4楼-- · 2019-03-08 12:02

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
查看更多
登录 后发表回答