Django Create ZIP File

Package your Django project for deployment

📦 What is a Deployment ZIP?

A deployment ZIP file packages your entire Django project into a single compressed file for easy upload to hosting platforms like AWS Elastic Beanstalk, making deployment simple and organized.


# Create a ZIP file of your project
zip -r myproject.zip . -x "*.git*" "*.pyc" "__pycache__/*" "venv/*"

# Or use EB CLI (automatic)
eb deploy
                                    

Why Create a ZIP File?

🚀

Easy Deployment

Upload single file to server

Simple Fast Convenient
📦

Complete Package

All files in one place

Organized Complete Portable
💾

Backup

Save project snapshots

Version control Safe Recoverable
🔄

Transfer

Share project easily

Shareable Compressed Efficient

🔹 What to Include in ZIP

Your deployment ZIP should contain only necessary files for running the application. Exclude development files, caches, and sensitive data to keep the package clean and secure.

✅ Include These:

  • All Python files (.py)
  • requirements.txt
  • .ebextensions/ folder (if using EB)
  • Static files (CSS, JS, images)
  • Templates folder
  • manage.py
  • Configuration files

❌ Exclude These:

  • .git/ folder
  • venv/ or virtualenv/
  • __pycache__/ folders
  • *.pyc files
  • .env files (use environment variables)
  • Database files (db.sqlite3)
  • IDE files (.vscode, .idea)

🔹 Create ZIP on Linux/Mac

Use the zip command to create a compressed archive of your project while excluding unnecessary files:

# Navigate to project directory
cd /path/to/myproject

# Create ZIP excluding common files
zip -r myproject.zip . \
  -x "*.git*" \
  -x "*__pycache__*" \
  -x "*.pyc" \
  -x "*venv/*" \
  -x "*.env" \
  -x "db.sqlite3"

# Verify ZIP contents
unzip -l myproject.zip

# Check ZIP size
ls -lh myproject.zip

Output:

  adding: manage.py (deflated 45%)
  adding: myproject/ (stored 0%)
  adding: myproject/settings.py (deflated 58%)
  adding: myproject/urls.py (deflated 42%)
  adding: requirements.txt (deflated 35%)
  
-rw-r--r--  1 user  staff   2.5M Oct 15 10:30 myproject.zip

🔹 Create ZIP on Windows

Windows users can create ZIP files using PowerShell or the built-in compression tool:

# Using PowerShell
cd C:\path\to\myproject

# Create ZIP file
Compress-Archive -Path * -DestinationPath myproject.zip

# Exclude specific folders (manual selection)
# Right-click project folder > Send to > Compressed folder
# Then manually delete unwanted files from ZIP
# Using Git Bash on Windows
cd /c/path/to/myproject

zip -r myproject.zip . \
  -x "*.git*" \
  -x "*__pycache__*" \
  -x "*.pyc" \
  -x "*venv/*"

🔹 Using .gitignore for ZIP

Create a .gitignore file to define which files should be excluded. Many ZIP tools can use this file:

# .gitignore
*.pyc
__pycache__/
venv/
env/
.env
db.sqlite3
*.log
.DS_Store
.vscode/
.idea/
*.swp
staticfiles/
media/
# Create ZIP respecting .gitignore
git archive -o myproject.zip HEAD

# Or use a script
zip -r myproject.zip . [email protected]

🔹 Automated ZIP Creation Script

Create a shell script to automate the ZIP creation process with proper exclusions:

#!/bin/bash
# deploy.sh - Create deployment ZIP

PROJECT_NAME="myproject"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
ZIP_NAME="${PROJECT_NAME}_${TIMESTAMP}.zip"

echo "Creating deployment package: $ZIP_NAME"

zip -r $ZIP_NAME . \
  -x "*.git*" \
  -x "*__pycache__*" \
  -x "*.pyc" \
  -x "*venv/*" \
  -x "*env/*" \
  -x "*.env" \
  -x "db.sqlite3" \
  -x "*.log" \
  -x ".DS_Store" \
  -x "*.swp" \
  -x "*staticfiles/*" \
  -x "*media/*"

echo "Package created: $ZIP_NAME"
ls -lh $ZIP_NAME
# Make script executable
chmod +x deploy.sh

# Run script
./deploy.sh

🔹 Python Script for ZIP Creation

Use Python to create a ZIP file programmatically with custom exclusion rules:

# create_zip.py
import os
import zipfile
from datetime import datetime

def should_exclude(filename):
    """Check if file should be excluded"""
    exclude_patterns = [
        '__pycache__', '.pyc', '.git', 'venv', 
        'env', '.env', 'db.sqlite3', '.DS_Store',
        '.vscode', '.idea', 'staticfiles', 'media'
    ]
    return any(pattern in filename for pattern in exclude_patterns)

def create_deployment_zip(project_name):
    """Create deployment ZIP file"""
    timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
    zip_name = f'{project_name}_{timestamp}.zip'
    
    with zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) as zipf:
        for root, dirs, files in os.walk('.'):
            # Remove excluded directories
            dirs[:] = [d for d in dirs if not should_exclude(d)]
            
            for file in files:
                filepath = os.path.join(root, file)
                if not should_exclude(filepath):
                    zipf.write(filepath)
                    print(f'Added: {filepath}')
    
    print(f'\nZIP created: {zip_name}')
    print(f'Size: {os.path.getsize(zip_name) / 1024 / 1024:.2f} MB')

if __name__ == '__main__':
    create_deployment_zip('myproject')
# Run the script
python create_zip.py

🔹 Elastic Beanstalk Auto-ZIP

When using EB CLI, ZIP creation is automatic. EB handles packaging and deployment:

# EB automatically creates ZIP and deploys
eb deploy

# EB respects .ebignore file
# Create .ebignore (similar to .gitignore)
cat > .ebignore << EOF
*.pyc
__pycache__/
venv/
.env
db.sqlite3
*.log
EOF

# Deploy with custom message
eb deploy -m "Version 1.2.0 - Added new features"

🔹 Verify ZIP Contents

Always verify your ZIP file before deployment to ensure it contains the right files:

# List ZIP contents
unzip -l myproject.zip

# Extract to test folder
unzip myproject.zip -d test_deploy/

# Check file count
unzip -l myproject.zip | wc -l

# Search for specific files
unzip -l myproject.zip | grep requirements.txt

🔹 Best Practices

ZIP Creation Tips:

  • ✅ Always exclude virtual environments
  • ✅ Remove database files (use RDS in production)
  • ✅ Exclude .env files (use environment variables)
  • ✅ Include requirements.txt
  • ✅ Test ZIP by extracting and running locally
  • ✅ Keep ZIP under 512MB for EB
  • ✅ Use version numbers in ZIP names
  • ❌ Don't include compiled files (.pyc)
  • ❌ Don't include IDE configuration files

🧠 Test Your Knowledge

Which folder should you EXCLUDE from deployment ZIP?