Background
This article solves a common challenge: deploying Python applications in completely isolated environments with no internet access. This scenario is common in:
- Financial and military sectors
- Government agencies
- Healthcare and energy infrastructure
Three Solutions
Solution 1: Offline Package Installation
- Download all dependencies:
pip download -r requirements.txt -d ./offline_packages - Transfer the packages to the target environment
- Install:
pip install --no-index --find-links=./offline_packages -r requirements.txt
Solution 2: Virtual Environment Packaging
- Create a venv and install dependencies
- Use
venv-packto create a tar.gz archive - Transfer and extract on the target machine
Solution 3: Container Deployment
- Write a Dockerfile
- Export the image with
docker save - Import and run in the target environment
Installation Workflow
Full Installation
- Export all packages:
pip freeze > requirements.txt - Create a directory:
mkdir offline_packages - Download packages:
pip wheel -w offline_packages -r requirements.txt - Transfer to the target machine
- Install:
pip install --no-index --find-links=offline_packages -r requirements.txt
Partial Installation
- Manually create
requirements.txtwith specific packages - Format examples:
- Exact version:
openpyxl==3.0.9 - Minimum version:
pandas>=1.3.0 - Any version:
numpy
- Exact version:
- Install:
pip install -r requirements.txt
Tips
- Use
pip downloadinstead ofpip wheelwhen you only need.whlfiles without building - Ensure the Python version and OS architecture match between the online and offline environments
- For complex projects, consider using Docker to avoid environment mismatch issues