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

  1. Download all dependencies: pip download -r requirements.txt -d ./offline_packages
  2. Transfer the packages to the target environment
  3. Install: pip install --no-index --find-links=./offline_packages -r requirements.txt

Solution 2: Virtual Environment Packaging

  1. Create a venv and install dependencies
  2. Use venv-pack to create a tar.gz archive
  3. Transfer and extract on the target machine

Solution 3: Container Deployment

  1. Write a Dockerfile
  2. Export the image with docker save
  3. Import and run in the target environment

Installation Workflow

Full Installation

  1. Export all packages: pip freeze > requirements.txt
  2. Create a directory: mkdir offline_packages
  3. Download packages: pip wheel -w offline_packages -r requirements.txt
  4. Transfer to the target machine
  5. Install: pip install --no-index --find-links=offline_packages -r requirements.txt

Partial Installation

  1. Manually create requirements.txt with specific packages
  2. Format examples:
    • Exact version: openpyxl==3.0.9
    • Minimum version: pandas>=1.3.0
    • Any version: numpy
  3. Install: pip install -r requirements.txt

Tips

  • Use pip download instead of pip wheel when you only need .whl files 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