Skip to content

Python:Requirements

Categories

No Binary

pip install --no-binary=protobuf protobuf

위와 같은 기능을 하는 requirements.txt 파일 내용은 다음과 같다:

spam --no-binary=eggs
bacon --only-binary=eggs

requirements.txt vs constraints.txt

자세한 내용은 Python:Constraints#requirements.txt vs constraints.txt 항목 참조.

Version specifiers

~=같은 버전 지정 방법. 자세한 내용은 PEP 440#Version specifiers 항목 참조.

Requirements File Format

여러 사이트의 패키지를 검색하고 싶다면 아래와 같이 작성하면 된다.

# Torch
--find-links https://download.pytorch.org/whl/torch_stable.html
torch==1.5.0+cu101
torchvision==0.6.0+cu101

# Detectron
--find-links https://dl.fbaipublicfiles.com/detectron2/wheels/cu101/index.html
detectron2

Compare Operators

~= 연산자

Adding to the existing answers, I think it's very important to also mention that while

~=0.6.10 means >=0.6.10, ==0.6.*

Following is also true

~=0.6 means >=0.6, ==0.*

좀 복잡한 제약 조건

opencv-pythonpyproject.toml 파일을 보면 다음과 같다:

[build-system]
requires = [
  "cmake>=3.1",
  "numpy==1.13.3; python_version=='3.6' and platform_machine != 'aarch64' and platform_machine != 'arm64'",
  "numpy==1.17.0; python_version=='3.7' and platform_machine != 'aarch64' and platform_machine != 'arm64'",
  "numpy==1.17.5; python_version=='3.8' and platform_machine != 'aarch64' and platform_machine != 'arm64'",
  "numpy==1.19.3; python_version<'3.9' and sys_platform == 'linux' and platform_machine == 'aarch64'",
  "numpy==1.21.0; python_version<'3.9' and sys_platform == 'darwin' and platform_machine == 'arm64'",
  "numpy>=2.0.0;  python_version>='3.9'",
  "pip",
  "scikit-build>=0.14.0",
  "setuptools==59.2.0",
]

또는 setup.py 파일:

    install_requires = [
        'numpy>=1.13.3; python_version<"3.7"',
        'numpy>=1.17.0; python_version>="3.7"', # https://github.com/numpy/numpy/pull/13725
        'numpy>=1.17.3; python_version>="3.8"',
        'numpy>=1.19.3; python_version>="3.9"',
        'numpy>=1.21.2; python_version>="3.10"',
        'numpy>=1.19.3; python_version>="3.6" and platform_system=="Linux" and platform_machine=="aarch64"',
        'numpy>=1.21.0; python_version<="3.9" and platform_system=="Darwin" and platform_machine=="arm64"',
        'numpy>=1.21.4; python_version>="3.10" and platform_system=="Darwin"',
        "numpy>=1.23.5; python_version>='3.11'",
        "numpy>=1.26.0; python_version>='3.12'"
    ]

패키지 찾기 플래그

--index-url
pip의 기본 인덱스(PyPI)를 대체합니다. 이걸 지정하면 pip은 더 이상 https://pypi.org/simple 을 보지 않고, 지정한 URL 하나만 검색합니다. 하나만 지정 가능하다.
--extra-index-url
PEP 503 인덱스 서버 (PyPI와 같은 구조) 를 기본 PyPI 위에 추가 인덱스를 덧붙입니다. pip은 PyPI와 지정한 URL 둘 다 검색하고, 버전이 맞는 패키지를 찾으면 어디서든 가져옵니다. 여러 개 추가 가능
--no-index
모든 인덱스 서버 무시, --find-links만 사용
--find-links
단순히 wheel 파일이 나열된 HTML 페이지나 디렉토리를 가리킵니다.

보안 주의사항

--extra-index-url은 두 인덱스를 동시에 보기 때문에 dependency confusion 공격에 취약할 수 있습니다. 공격자가 PyPI에 사내 패키지와 같은 이름으로 더 높은 버전을 올리면, pip이 그걸 설치할 수 있습니다. 보안이 중요한 환경에서는 --index-url로 사내 미러만 지정하거나, --extra-index-url 대신 pip의 --require-hashes 옵션을 함께 쓰는 것이 권장됩니다.

--no-binary :all:
wheel 무시, 소스에서 빌드 강제
--only-binary :all:
sdist 무시, wheel만 허용 (빌드 안 함)
--no-binary<pkg>
특정 패키지만 소스 빌드 강제
--only-binary<pkg>
특정 패키지만 wheel 강제
--trusted-host
SSL 검증 건너뛸 호스트 지정 (사내 미러 등)

해시 검사

--require-hashes 를 추가하면 pip에서 모든 패키지의 해시값을 검증하도록 강제합니다:

# requirements.txt
--require-hashes

requests==2.31.0 \
    --hash=sha256:942c5a758f98d790eaed1a29cb6eefc7f0edf3fcb0fce8afe0f44064ad1e1b68

certifi==2024.2.2 \
    --hash=sha256:dc383c07b76109f368f6106eee2b593b04a011ea4d55f652c6ca24a754d1cdd1

다른 requirements.txt 포함

-r requirements-dev.txt
-r requirements-test.txt

See also

Favorite site