Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Zipping Packages

00:00 In the previous lesson, I showed you some of the tools in importlib for dynamically playing with modules. In this lesson, I’ll show you another way of bundling up your code using a zip file.

00:11 Python actually knows how to deal with zip files and you can zip up your scripts and run them directly on the command line. In fact, Python has a utility called zipapp that will take a script file and create a zip bundle automatically.

00:24 When it does this, it even creates a __main__.py file inside of it so that the zip can be created as a module and run. Let’s go zip some code.

00:35 I’m going to start with a very simple case. Remember from the last lesson that if you have a file named __main__.py, Python considers it runnable when associated with a module.

00:46 Well, the inside of a runnable zip works the same. Here, I’ve got a __main__.py file with a single print() statement. Now all I have to do is zip it up.

01:00 This is the format of the zip command on Unix-like systems. The first argument is the name of the zip file, which by convention gets called .pyz for Python zip.

01:11 The second argument is what to be zipped, which in this case is the __main__.py file. That done, I can use Python on the file, and there you go.

01:24 Python speaks zip as well. Note the file location here. The zippy/ directory is me just keeping the example code separate from everything else, and it considers the __main__.py file to be under hello.pyz.

01:39 Let’s make this slightly more complicated. What fun is hello without a long goodbye? Here is my directory structure. It has a single Python file called talk.py.

01:50 Let’s look inside of it.

01:53 talk.py has a single function named main(). This is a convention for the function you call from within a script. It isn’t required here, but it’s common practice.

02:03 Contents of the main() function is similar to what I just showed you with the hello zip. That’s it for the code. Now I want to create a runnable zip.

02:12 Since this time, there’s no __main__.py file, I’m going to use the Python zipapp utility, which is itself a runnable module, so I get at it with that same -m argument.

02:29 I find this line a little confusing. The first -m is to Python saying to run the zipapp module. The goodbye argument is the directory that the zipapp module is going to zip up.

02:41 The second -m has nothing to do with modules. It’s being passed to the zipapp script and is used to indicate what the runnable function is to convert to a __main__.py file.

02:53 The notation talk:main means to use the function called main() in the file called talk as the entry point when the zip file gets run.

03:03 The result of this zipapp command is a new file named goodbye.pyz. Let me run it, and there you go. Goodbye. If you look inside goodbye.pyz, you’ll find that it has created a __main__.py file for you, which imports talk and then calls the main() function inside of it.

03:25 On a Windows machine, the .pyz extension is associated with Python, so if you double-click it, it will run like any other Python script. On Unix-based systems, there is no such file association mechanism.

03:38 You have two choices here. You can either call Python directly like I did in the examples, or you can shebang it. If you’re a Unix person, you’ve probably seen the hash bang indicator at the top of a script file that tells Unix what interpreter to use to run the script file.

03:54 That hash bang is colloquially called shebang, short for shell bang, and it tells the shell what to run.

04:01 The zipapp utility takes an optional -p argument, which can set a shebang value associated with the zip file. In this example, I’m using the -m utility to launch Python.

04:14 If you create a .pyz this way, you can use the ./ execution shortcut. It even makes the file executable for you, so you don’t have to change on it. If you’re a Windows person, that last bit probably sounded like nonsense.

04:28 Short version is there’s a way to make it executable, the Unix equivalent of double-clicking.

04:34 It’s important to note that this is Python, running a zip file with scripts in it. It is not bundling Python. Python has to be installed on the system for this to work.

04:44 Bundling platform-independent executables is a whole other kettle of fish.

04:50 Next up, a series of short tips and tricks for your importing pleasure.

Become a Member to join the conversation.