Beginner’s Guide to Packaging Python Programs: A Dual-Platform Guide for Windows/Linux

📦 Beginner’s Guide to Packaging Python Programs: A Dual-Platform Guide for Windows/Linux

🌟 Why Package?

✅ Users do not need to install the Python environment ✅ Protect source code ✅ One-click installation/uninstallation is more convenient

🖥 Windows Platform: Packaging EXE Files

Recommended Tool: PyInstaller (Five-Star Rated Tool)

# One-click installation
pip install pyinstaller

🚀 3 Steps to Generate EXE

  1. Basic Packaging (Generate a Single File)

pyinstaller --onefile your_script.py

📍 The generated file is located in the <span>dist/</span> directory

  1. Advanced Customization (Select as Needed)

# Add application icon
pyinstaller --onefile --icon=app.ico your_script.py

# Hide the black window (suitable for GUI programs)
pyinstaller --windowed your_script.py

# Include image and other resource files
pyinstaller --add-data "images/*.png;images" your_script.py
  1. Resource File Loading Tips Add in the code:

import sys
import os

def resource_path(relative_path):
    """ Get the absolute path of the resource"""
    if hasattr(sys, '_MEIPASS'):
        return os.path.join(sys._MEIPASS, relative_path)
    return os.path.join(os.path.abspath("."), relative_path)

# Usage example
image_path = resource_path("images/logo.png")

🐧 Linux Platform: Packaging DEB Files

Option 1: Manual Build (Suitable for Beginners)

# Create standard directory structure
mkdir -p myapp/DEBIAN myapp/usr/bin myapp/usr/share/myapp

📁 Key File Configuration:

myapp/DEBIAN/control  👇
---------------------------------
Package: myapp
Version: 1.0
Maintainer: Zhang San <[email protected]>
Description: My First Python Program
Architecture: all

📌 Startup Script Configuration:

# Create launcher /usr/bin/myapp
#!/bin/bash
python3 /usr/share/myapp/main.py "$@"

# Remember to add execution permissions!
chmod +x myapp/usr/bin/myapp

🔨 Final Packaging Command:

dpkg-deb --build myapp

Option 2: Automated Tool (Recommended for Advanced Users)

# Install stdeb tool
pip install stdeb

# Write setup.py and execute
python3 setup.py bdist_deb

💡 Expert Suggestions

  1. Virtual Environment: Use<span>virtualenv</span> to isolate dependencies

  2. Dependency Management: Record all dependencies in<span>requirements.txt</span>

  3. Size Optimization: Use UPX compression (can reduce size by 50%)

  • Download link:UPX Official Website

  • Compatibility Testing: Test in a clean virtual machine

  • 🚨 Common Issues

    ❓ What to do if the program crashes after packaging? 👉 Try removing the<span>--onefile</span> parameter to check the error log

    ❓ Icon not working? 👉 Ensure the icon is in .ico format, recommended size 256×256

    ❓ DEB installation failed? 👉 Check the control file format (must use space indentation)

    📚 Learning Resources

    • PyInstaller Official Documentation: Link

    • Debian Packaging Guide: Link

    Follow us for more technical tips Feel free to leave comments for practical issues 💬

    Leave a Comment