Tutorialspoint

1 Answer
Arushi

The zipapp module has been introduced in Python's standard library since ver 3.5. This module is used to manage the creation of zip files containing Python code, which can be executed directly by the Python interpreter. The module provides both a Command-Line Interface and a programming interface.

To use zipapp module programmatically, we should have a module in which main function is present. The executable archive is built by following command −

python -m zipapp myapp -m "example:main"

Here, the current path should have a folder called myapp. In this folder, there should be example.py which must have main() function.

Create myapp folder and save following code as example.py in it −

def main():
   print ('Hello World')
if __name__=='__main__':
   main()

When above command is executed from command terminal, it will create myapp.pyz. We can now execute it from the command prompt.

C:\python37>python myapp.pyz
Hello World

Following command line options are supported −

-o <output>, --output=<output>

By default, output file name carries .pyz extension and name as same as the input source. It can be changed by specifying in -o option.

-p <interpreter>, --python=<interpreter>

This is used to explicitly specifying interpreter as the command to run.

-c, --compress

Compress files with the deflate method, reducing the size of the output file.

The zipapp module can be used programmatically. It defines the following functions −

zipapp.create_archive(source)

Create an application archive from source folder. Additionally, interpreter, target and compressed arguments can be used.

zipapp.get_interpreter(archive)

Return the interpreter specified in the #! line at the start of the archive.

>>> import zipapp
>>> zipapp.create_archive('myapp.pyz', 'myapp')

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.