Bundling a Multiple Module Python Project Into a Zip File

Python

A Python project with multiple modules can be bundled into a single .zip file for easy deployment, similar to a JAR file. This doesn’t manage dependencies (like Freeze or py2exe) but for systems where you know that the project dependencies (and a Python interpreter) are already installed, it makes using the project much easier.

Here’s a simple example. Let’s say you have a Python project with two modules: __main__.py and mygreeting.py. (For the .zip bundle to work, the entry point must be in a file named __main__.py)

main.py

import mygreeting
import sys
    
arg_count = len(sys.argv)
    
print('Number of arguments:', arg_count, 'argument(s).')
print('Argument List:', str(sys.argv))
    
greeter = mygreeting.MyGreeter()
    
greeter.say_hello("" if arg_count == 1 else sys.argv[1])

mygreeting.py

class MyGreeter:
    def __init__(self) -> None:
        pass
    
    def say_hello(self, name = ""):
        print("Hello there!" if name == "" else f"Hello, {name}!")

Compress all of the source files into a .zip file:

zip myapp.zip *.py

Then, execute the bundle with the Python interpreter:

python3 myapp.zip

Output:

Number of arguments: 1 argument(s).
Argument List: ['myapp.zip']
Hello there!

You can also pass arguments to the bundle:

python3 myapp.zip Jim

Output:

Number of arguments: 2 argument(s).
Argument List: ['myapp.zip', 'Jim']
Hello, Jim!

You can also make a bundle that can be run directly:

echo '#!/usr/bin/env python3' | cat - myapp.zip > myapp
 
chmod u+x myapp

Run it:

./myapp

Same output:

Number of arguments: 1 argument(s).
Argument List: ['myapp.zip']
Hello there!

You can copy the .zip or executable bundle anywhere on your system as a single file, and it will be runnable. Very handy!