Session Ready
Exercise

Zipapp

In this exercise, we will

  1. zip up a project called myproject
  2. make the zipped project command-line executable
  3. create a __main__.py file in the zipped project

all with a single call to the create_archive() function from the standard library zipapp module.

The python interpreter we want to use is /usr/bin/env python,

while the function we want __main__.py to run is called print_name_and_file():

def print_name_and_file():
    print(f"Name is {__name__}. File is {__file__}.")

The print_name_and_file() function is in the mymodule.py file inside the top-level mypackage directory, as shown below:

myproject
└── mypackage
    ├── __init__.py
    └── mymodule.py
Instructions
100 XP
  • Use the create_archive() function to zip up a project called myproject.
  • Pass a string that points to the print_name_and_file() as the main argument to generate a __main__.py file.