IT story

requirements.txt로 설치할 때 단일 패키지에서 pip가 실패하지 않도록 방지

hot-time 2020. 8. 23. 10:00
반응형

requirements.txt로 설치할 때 단일 패키지에서 pip가 실패하지 않도록 방지


requirements.txt에서 패키지를 설치하고 있습니다.

pip install -r requirements.txt

requirements.txt 파일은 다음과 같습니다.

Pillow
lxml
cssselect
jieba
beautifulsoup
nltk

lxml설치에 실패한 유일한 패키지이며 이로 인해 모든 것이 실패합니다 (주석에서 larsk가 지적한 예상 결과). 그러나 lxml실패 pip후에도 나머지 패키지는 계속 실행되고 다운로드됩니다.

내가 이해하는 바에 pip install -r requirements.txt따르면에 나열된 패키지 중 하나라도 설치에 실패하면 명령이 실패합니다 requirements.txt.

실행할 pip install -r requirements.txt수있는 것을 설치하고 할 수없는 패키지를 건너 뛰거나 무언가가 실패하는 즉시 종료하도록 명령하기 위해 실행할 수있는 인수 가 있습니까?


각 줄을 실행 pip install하면 해결 방법이 될 수 있습니다.

cat requirements.txt | xargs -n 1 pip install

참고 : -aMacOS에서는 매개 변수를 사용할 수 없으므로 오래된 고양이가 더 휴대 성이 좋습니다.


Windows의 경우 :

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 버전 <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)

xargs당신이 당신의 요구 사항 파일에 주석이나 빈 줄이있는 경우 솔루션 작품은 성가신을 이식성 문제 (BSD / GNU)가 및 / 또는 수 있지만.

이러한 동작이 필요한 사용 사례의 경우, 예를 들어 두 개의 개별 요구 사항 파일을 사용합니다. 하나는 항상 설치해야하는 핵심 종속성 만 나열하는 파일이고 다른 하나는 케이스의 90 %에 해당하는 비 핵심 종속성이있는 파일입니다. 대부분의 사용 사례에는 필요하지 않습니다. 이것은 Recommends데비안 패키지 섹션과 동일 합니다.

다음 쉘 스크립트 (필수 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

This solution handles empty lines, whitespace lines, # comment lines, whitespace-then-# comment lines in your requirements.txt.

cat requirements.txt | sed -e '/^\s*#.*$/d' -e '/^\s*$/d' | xargs -n 1 pip install

Hat tip to this answer for the sed magic.


Thanks, Etienne Prothon for windows cases.

But, after upgrading to pip 18, pip package don't expose main to public. So you may need to change code like this.

 # 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)

For 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()
  1. Create a local directory, and put your requirements.txt file in it.
  2. Copy the code above and save it as a python file in the same directory. Remember to use .py extension, for instance, install_packages.py
  3. Run this file using a cmd: python install_packages.py
  4. All the packages mentioned will be installed in one go without stopping at all. :)

You can add other parameters in install function. Like: main(['install'] + [str(package)] + ['--update'])


Do you have requirements for lxml using? Here they are for install:

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

If you use Windows or Mac, you can check that too. Alternatively, setting STATIC_DEPS=true will download and build both libraries automatically.(c)
http://lxml.de/installation.html

참고URL : https://stackoverflow.com/questions/22250483/stop-pip-from-failing-on-single-package-when-installing-with-requirements-txt

반응형