在使用requirements.txt进行安装时,停止单点失败

我正在从requirements.txt安装软件包

pip install -r requirements.txt

requirements.txt文件如下所示:

Pillow lxml cssselect jieba beautifulsoup nltk 

lxml是唯一没有安装的软件包,导致一切都失败(注释中的larsks指出了预期的结果)。 但是,在lxml失败之后, pip仍会运行并下载其余的软件包。

从我所了解的pip install -r requirements.txt命令将会失败,如果任何在requirements.txt列出的软件包都无法安装。

当运行pip install -r requirements.txt告诉它安装它可以跳过的软件包,或者在它看到某些东西失败时立即退出时,是否有任何参数可以通过?

pip install来运行每一行可能是一个解决方法。

 cat requirements.txt | xargs -n 1 pip install 

注意: -a参数在MacOS下不可用,所以老猫更便携。

xargs解决scheme可以工作,但如果您的需求文件中有注释或空行,可能会有可移植性问题(BSD / GNU)和/或麻烦。

至于需要这种行为的用例,我使用了两个单独的需求文件,一个是只列出需要总是安装的核心依赖关系,另一个是非核心依赖关系,在90%的情况下大多数用具不需要。 这相当于debian软件包的“ Recommends部分。

我使用下面的shell脚本(需要sed )来安装可选的依赖项

 #!/bin/sh while read dependency; do dependency_stripped="$(echo "${dependency}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" # Skip comments if [[ $dependency_stripped == \#* ]]; then continue # Skip blank lines elif [ -z "$dependency_stripped" ]; then continue else if pip install "$dependency_stripped"; then echo "$dependency_stripped is installed" else echo "Could not install $dependency_stripped, skipping" fi fi done < recommends.txt 

对于Windows:

 # This code install line by line a list of pip package import sys import pip def install(package): pip.main(['install', package]) if __name__ == '__main__': with open(sys.argv[1]) as f: for line in f: install(line) 

你有使用lxml的要求吗? 在这里他们是为了安装:

 sudo apt-get install libxml2-dev libxslt-dev python-dev 

如果你使用Windows或Mac,你也可以检查。 或者,设置STATIC_DEPS = true将自动下载并build立这两个库。(c)
http://lxml.de/installation.html