The PyPA recommended tool for installing Python packages.
91
Comprehensive package building, distribution, and cryptographic verification capabilities for development and deployment workflows.
Build wheel distributions from source packages and requirements.
# Build wheel from current directory
pip wheel .
# Build wheel from specific directory
pip wheel /path/to/package/
# Build wheels from requirements
pip wheel -r requirements.txt
# Build wheel to specific directory
pip wheel --wheel-dir /path/to/wheels package_name
# Build without dependencies
pip wheel --no-deps package_name
# Build from source (no binary wheels)
pip wheel --no-binary :all: package_name
# Build only binary wheels
pip wheel --only-binary :all: package_name
# Build with constraints
pip wheel -c constraints.txt -r requirements.txtWheel building options:
# Build configuration
pip wheel --global-option="--plat-name" --global-option="linux_x86_64" .
pip wheel --build-option="--debug" package_name
# Index options
pip wheel --index-url https://pypi.org/simple/ package_name
pip wheel --extra-index-url https://test.pypi.org/simple/ package_name
pip wheel --find-links /path/to/wheels package_name
# Caching and build isolation
pip wheel --no-cache-dir package_name
pip wheel --no-build-isolation package_name
# Verbosity
pip wheel --verbose package_name
pip wheel --quiet package_nameDownload packages without installing them for offline installation or distribution.
# Download package and dependencies
pip download package_name
# Download to specific directory
pip download --dest /path/to/downloads package_name
# Download from requirements file
pip download -r requirements.txt
# Download without dependencies
pip download --no-deps package_name
# Download source distributions only
pip download --no-binary :all: package_name
# Download binary wheels only
pip download --only-binary :all: package_name
# Download specific platform
pip download --platform linux_x86_64 --only-binary=:all: package_name
# Download for specific Python version
pip download --python-version 3.9 --only-binary=:all: package_name
# Download for specific ABI
pip download --abi cp39 --only-binary=:all: package_namePlatform and architecture options:
# Platform-specific downloads
pip download --platform win_amd64 package_name
pip download --platform macosx_10_9_x86_64 package_name
pip download --platform linux_x86_64 package_name
pip download --platform any package_name
# Implementation-specific
pip download --implementation cp package_name # CPython
pip download --implementation pp package_name # PyPyGenerate cryptographic hashes for package files to ensure integrity and security.
# Compute hash for single file
pip hash package.tar.gz
# Compute hashes for multiple files
pip hash package1.tar.gz package2.whl
# Specify hash algorithm
pip hash --algorithm sha256 package.tar.gz
pip hash --algorithm sha384 package.tar.gz
pip hash --algorithm sha512 package.tar.gz
# Hash remote files
pip hash https://files.pythonhosted.org/packages/source/r/requests/requests-2.28.1.tar.gzHash output format:
package.tar.gz:
--hash=sha256:abcd1234efgh5678ijkl9012mnop3456qrst7890uvwx1234yz567890abcdef12Using hashes in requirements files:
requests==2.28.1 \
--hash=sha256:7c5599b102feddaa661c826c56ab4fee28bfd17f5abca1ebbe3e7f19d7c97ddf \
--hash=sha256:8fefa2a1a1365bf5520aac41836fbee479da67864514bdb821f31ce07ce65349
django==4.1.0 \
--hash=sha256:a153ffd5143bf26a877bfae2f4ec736ebd8924a46600ca089ad96b54a1d4e28e \
--hash=sha256:acb21faa51d5fb02fe81c51e5b9c7403dc71dcb0bd8d5e4a8dced2c1b0aa6272Create offline installation packages and perform installations without internet access.
# Download packages for offline installation
mkdir offline_packages
pip download -d offline_packages -r requirements.txt
# Install from offline packages
pip install --find-links offline_packages --no-index -r requirements.txt
# Create complete offline bundle
pip download --dest offline_bundle \
--requirement requirements.txt \
--no-binary :all: # Include source distributionsBuild and install packages in development mode for active development.
# Editable installation (development mode)
pip install -e .
pip install -e /path/to/package/
# Build wheel in development mode
pip wheel -e .
# Install with extras
pip install -e ".[dev,test]"
# Install with constraints in development mode
pip install -e . -c constraints.txtWork with different Python build systems and build backends.
# Build isolation control
pip wheel --no-build-isolation package_name # Use system packages
pip wheel --build-isolation package_name # Isolated build (default)
# Build backend configuration
# Controlled by pyproject.toml:
# [build-system]
# requires = ["setuptools>=45", "wheel", "setuptools_scm[toml]>=6.2"]
# build-backend = "setuptools.build_meta"
# Legacy setup.py builds
pip wheel --use-pep517 package_name # Use PEP 517 (default)
pip wheel --no-use-pep517 package_name # Use legacy setup.pyUse subprocess for programmatic package building and distribution.
import subprocess
import sys
import os
from pathlib import Path
def build_wheel(package_path, wheel_dir=None, no_deps=False):
"""Build wheel from package directory."""
cmd = [sys.executable, '-m', 'pip', 'wheel']
if wheel_dir:
cmd.extend(['--wheel-dir', str(wheel_dir)])
if no_deps:
cmd.append('--no-deps')
cmd.append(str(package_path))
try:
subprocess.check_call(cmd)
print(f"Successfully built wheel for {package_path}")
except subprocess.CalledProcessError as e:
print(f"Failed to build wheel: {e}")
raise
def download_packages(requirements_file, dest_dir):
"""Download packages for offline installation."""
cmd = [
sys.executable, '-m', 'pip', 'download',
'--dest', str(dest_dir),
'-r', str(requirements_file)
]
try:
subprocess.check_call(cmd)
print(f"Downloaded packages to {dest_dir}")
except subprocess.CalledProcessError as e:
print(f"Failed to download packages: {e}")
raise
def compute_hash(file_path, algorithm='sha256'):
"""Compute hash for package file."""
cmd = [
sys.executable, '-m', 'pip', 'hash',
'--algorithm', algorithm,
str(file_path)
]
try:
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
return result.stdout.strip()
except subprocess.CalledProcessError as e:
print(f"Failed to compute hash: {e}")
return None
def create_offline_bundle(requirements_file, bundle_dir):
"""Create complete offline installation bundle."""
bundle_path = Path(bundle_dir)
bundle_path.mkdir(exist_ok=True)
# Download packages
download_packages(requirements_file, bundle_path)
# Create installation script
install_script = bundle_path / 'install.py'
script_content = f'''#!/usr/bin/env python3
import subprocess
import sys
from pathlib import Path
def main():
bundle_dir = Path(__file__).parent
requirements_file = bundle_dir / "requirements.txt"
cmd = [
sys.executable, '-m', 'pip', 'install',
'--find-links', str(bundle_dir),
'--no-index',
'-r', str(requirements_file)
]
subprocess.check_call(cmd)
print("Offline installation completed successfully")
if __name__ == "__main__":
main()
'''
with open(install_script, 'w') as f:
f.write(script_content)
# Copy requirements file
import shutil
shutil.copy2(requirements_file, bundle_path / 'requirements.txt')
print(f"Offline bundle created in {bundle_dir}")
print(f"To install: cd {bundle_dir} && python install.py")
def build_distribution_package(package_dir, output_dir=None):
"""Build both source and wheel distributions."""
package_path = Path(package_dir)
output_path = Path(output_dir) if output_dir else package_path / 'dist'
output_path.mkdir(exist_ok=True)
# Build wheel
wheel_cmd = [
sys.executable, '-m', 'pip', 'wheel',
'--wheel-dir', str(output_path),
'--no-deps',
str(package_path)
]
try:
subprocess.check_call(wheel_cmd)
print(f"Built wheel distribution in {output_path}")
except subprocess.CalledProcessError as e:
print(f"Failed to build wheel: {e}")
raise
# Build source distribution (if setup.py exists)
setup_py = package_path / 'setup.py'
if setup_py.exists():
sdist_cmd = [
sys.executable, str(setup_py), 'sdist',
'--dist-dir', str(output_path)
]
try:
subprocess.check_call(sdist_cmd, cwd=package_path)
print(f"Built source distribution in {output_path}")
except subprocess.CalledProcessError as e:
print(f"Failed to build source distribution: {e}")
# Usage examples
build_wheel('.', wheel_dir='./dist')
download_packages('requirements.txt', './offline_packages')
hash_output = compute_hash('package.tar.gz')
print(hash_output)
create_offline_bundle('requirements.txt', './offline_bundle')
build_distribution_package('.', './dist')Integrate package building into continuous integration and deployment pipelines.
# GitHub Actions example build step
name: Build Package
run: |
python -m pip install --upgrade pip build
python -m pip wheel --wheel-dir dist .
python -m pip hash dist/*.whl
# Jenkins pipeline build step
stage('Build Package') {
steps {
sh 'python -m pip wheel --wheel-dir dist .'
sh 'python -m pip hash dist/*.whl > dist/hashes.txt'
archiveArtifacts artifacts: 'dist/*', fingerprint: true
}
}
# Docker multi-stage build
FROM python:3.9 as builder
COPY requirements.txt .
RUN pip wheel --wheel-dir /wheels -r requirements.txt
FROM python:3.9-slim
COPY --from=builder /wheels /wheels
COPY requirements.txt .
RUN pip install --find-links /wheels --no-index -r requirements.txtInstall with Tessl CLI
npx tessl i tessl/pypi-pipevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10