2017-11-30 1 views
2

공개 다운로드 위치가없는 독점적 종속성이있는 패키지 작업 중입니다. pip이 특정 종속성이 설치되어 있지 않으면 설치를 중단하거나 경고를 인쇄하고 종속성을 제거한 상태로 계속 진행하려고합니다. 내 마지막에 아마 setup.py으로 구성하고 싶습니다. 내 패키지를 설치할 때 사용자가 지정해야하는 것이 아닙니다.Undownloadable dependencies

나는 특히 삐뚤어지기를 원하지 않는다. 을 다운로드하거나 어디서나 종속성을 설치하려고한다. 특히 pip가 PyPI에서 종속성을 다운로드하려고하면 누군가가 종속성 이름에 나쁜 것을 등록 할 수 있고 pip는이를 설치할 것입니다.

alternate download links을 PyPI 이외의 곳에서 다운로드해야하는 종속성을 지정하는 방법이 있지만 종속성을 전혀 다운로드하지 말아야한다는 것을 발견하지 못했습니다.

가장 좋은 해결 방법은 install_requires 목록에 종속성을 지정하지 않고 종속성없이 패키지를 설치하면 종속성이 있음을 나타내는 표시가없고 경고가 표시되지 않습니다.

특정 종속성을 다운로드해서는 안된다고 말하는 방법이 있습니까? 핍이 특정 의존성이 설치되지 않았 음을 발견하면

답변

3

, 나는 내가 제대로 이해하면 고유의 패키지가 설치되어있는 경우

, 당신은 확인할 수 있습니다 ...는 설치를 중단하거나 원하는 setup.py에있는 해당 패키지에서 모듈을 가져 오려고하면 ImportError에서 중단됩니다.

$ python setup.py sdist 

: 바퀴 설치에 setup.py를 호출하지 않기 때문에 당신은 소스 타르와 같은 패키지를 배포해야 지금

from distutils.command.build import build as build_orig 
from distutils.errors import DistutilsModuleError 
from setuptools import setup 


class build(build_orig): 

    def finalize_options(self): 
     try: 
      import numpy 
     except ImportError: 
      raise DistutilsModuleError('numpy is not installed. Installation will be aborted.') 
     super().finalize_options() 


setup(
    name='spam', 
    version='0.1', 
    author='nobody', 
    author_email='[email protected]', 
    packages=[], 
    install_requires=[ 
     # all the other dependencies except numpy go here as usual 
    ], 
    cmdclass={'build': build,}, 
) 

: 예를 들어,의 설치가 numpy입니다 중단해야 당신의 의존성을 가정 해 봅시다 내가 할 수있는

$ pip install dist/spam-0.1.tar.gz 
Processing ./dist/spam-0.1.tar.gz 
    Complete output from command python setup.py egg_info: 
    running egg_info 
    creating pip-egg-info/spam.egg-info 
    writing pip-egg-info/spam.egg-info/PKG-INFO 
    writing dependency_links to pip-egg-info/spam.egg-info/dependency_links.txt 
    writing top-level names to pip-egg-info/spam.egg-info/top_level.txt 
    writing manifest file 'pip-egg-info/spam.egg-info/SOURCES.txt' 
    reading manifest file 'pip-egg-info/spam.egg-info/SOURCES.txt' 

    error: numpy is not installed. Installation will be aborted. 

    ---------------------------------------- 
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/_y/2qk6029j4c7bwv0ddk3p96r00000gn/T/pip-s8sqn20t-build/ 

참고 : numpy가 발생합니다 누락 된 내장 된 타르를 설치하려고 설치 스크립트의 상단에 대한 수입 검사 :

from setuptools import setup 

try: 
    import numpy 
except ImportError: 
    raise DistutilsModuleError(...) 

setup(...) 

그러나이 경우

, 포맷되지 않습니다 출력과 전체 스택 추적도 기술과 사용자를 혼란스럽게 할 수있는 표준 출력에 유출 될 것이다. 대신 설치시 호출 될 distutils 명령 중 하나를 서브 클래스화하여 오류 출력의 형식이 올바로 지정되도록하십시오. 이제

, 두 번째 부분 :

... 또는 경고를 인쇄하고 제거 종속성을 계속합니다.

버전 7 pip will swallow any output from your setup script 이후 더 이상 가능하지 않습니다. pip가 상세 모드 (예 : pip install -v mypkg)로 실행되는 경우에만 사용자는 설정 스크립트의 출력을 볼 수 있습니다. 네가 나 한테 물어 보면 의심스러운 결정이야.

from distutils.log import ERROR 

class build(build_orig): 

    def finalize_options(self): 
     try: 
      import numpy 
     except ImportError: 
      # issue an error and proceed 
      self.announce('Houston, we have an error!', level=ERROR) 
      # for warnings, there is a shortcut method 
      self.warn('I am warning you!') 
     super().finalize_options() 
+0

'cmdclass'에 대한 모든 문서가 있는가 :

그럼에도 불구하고, 여기에서 설치 스크립트에 경고 및 오류를 발행하는 예입니다? 나는 그것을 발견 할 수 없다. – user2357112

+0

@ user2357112 그것은 파이썬의 stdlib의 일부입니다. [Distutils 확장하기] (https://docs.python.org/3.6/distutils/extending.html)는 좋은 시작점입니다. – hoefling