Python 使用 requirements.txt 安装时,阻止 pip 在单个包上失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22250483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stop pip from failing on single package when installing with requirements.txt
提问by emh
I am installing packages from requirements.txt
我正在安装软件包 requirements.txt
pip install -r requirements.txt
The requirements.txtfile reads:
该requirements.txt文件写道:
Pillow
lxml
cssselect
jieba
beautifulsoup
nltk
lxmlis the only package failing to install and this leads to everything failing (expected results as pointed out by larsks in the comments). However, after lxmlfails pipstill runs through and downloads the rest of the packages.
lxml是唯一无法安装的软件包,这导致一切都失败(如 larsks 在评论中指出的预期结果)。但是,lxml失败后pip仍然运行并下载其余的包。
From what I understand the pip install -r requirements.txtcommand will fail if any of the packages listed in the requirements.txtfail to install.
据我了解,pip install -r requirements.txt如果requirements.txt安装失败中列出的任何软件包,该命令将失败。
Is there any argument I can pass when running pip install -r requirements.txtto tell it to install what it can and skip the packages that it cannot, or to exit as soon as it sees something fail?
在运行时我是否可以通过任何参数pip install -r requirements.txt来告诉它安装它可以安装的东西并跳过它不能安装的包,或者在它看到某些东西失败时立即退出?
采纳答案by MZD
Running each line with pip installmay be a workaround.
运行每一行pip install可能是一种解决方法。
cat requirements.txt | xargs -n 1 pip install
Note: -aparameter is not available under MacOS, so old cat is more portable.
注意:-a参数在MacOS下不可用,所以老猫更便携。
回答by Leo Cavaille
The xargssolution works but can have portability issues (BSD/GNU) and/or be cumbersome if you have comments or blank lines in your requirements file.
该xargs解决方案有效,但可能存在可移植性问题 (BSD/GNU) 和/或麻烦,如果您的需求文件中有注释或空行。
As for the usecase where such a behavior would be required, I use for instance two separate requirement files, one which is only listing core dependencies that need to be always installed and another file with non-core dependencies that are in 90% of the cases not needed for most usecases. That would be an equivalent of the Recommendssection of a debian package.
至于需要这种行为的用例,我使用了例如两个单独的需求文件,一个只列出了需要始终安装的核心依赖项,另一个包含 90% 情况下的非核心依赖项的文件大多数用例不需要。这相当于Recommendsdebian 包的部分。
I use the following shell script (requires sed) to install optional dependencies:
我使用以下 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
回答by Etienne Prothon
For Windows:
对于 Windows:
pip version >=18
pip 版本 >=18
import sys
from pip._internal import main as pip_main
def install(package):
pip_main(['install', package])
if __name__ == '__main__':
with open(sys.argv[1]) as f:
for line in f:
install(line)
pip version <18
pip 版本 <18
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)
回答by Jaeyoon Jeong
Thanks, Etienne Prothon for windows cases.
谢谢,Etienne Prothon 用于 Windows 机箱。
But, after upgrading to pip 18, pip package don't expose main to public. So you may need to change code like this.
但是,升级到 pip 18 后,pip 包不会向公众公开 main。所以你可能需要像这样更改代码。
# This code install line by line a list of pip package
import sys
from pip._internal import main as pip_main
def install(package):
pip_main(['install', package])
if __name__ == '__main__':
with open(sys.argv[1]) as f:
for line in f:
install(line)
回答by rouble
This solution handles empty lines, whitespace lines, # comment lines, whitespace-then-# comment lines in your requirements.txt.
此解决方案处理您的requirements.txt 中的空行、空格行、# 注释行、空格然后-# 注释行。
cat requirements.txt | sed -e '/^\s*#.*$/d' -e '/^\s*$/d' | xargs -n 1 pip install
Hat tip to this answerfor the sed magic.
对于 sed 魔法的这个答案的帽子提示。
回答by Pranzell
For Windows:
对于 Windows:
import os
from pip.__main__ import _main as main
error_log = open('error_log.txt', 'w')
def install(package):
try:
main(['install'] + [str(package)])
except Exception as e:
error_log.write(str(e))
if __name__ == '__main__':
f = open('requirements1.txt', 'r')
for line in f:
install(line)
f.close()
error_log.close()
- Create a local directory, and put your
requirements.txtfile in it. - Copy the code above and save it as a python file in the same directory. Remember to use
.pyextension, for instance,install_packages.py - Run this file using a cmd:
python install_packages.py - All the packages mentioned will be installed in one go without stopping at all. :)
- 创建一个本地目录,并将您的
requirements.txt文件放入其中。 - 复制上面的代码并在同一目录下将其另存为python文件。记得使用
.py扩展名,例如,install_packages.py - 使用 cmd 运行此文件:
python install_packages.py - 提到的所有软件包都将一次性安装,根本不会停止。:)
You can add other parameters in install function. Like:
main(['install'] + [str(package)] + ['--update'])
您可以在 install 函数中添加其他参数。喜欢:
main(['install'] + [str(package)] + ['--update'])

