Create Python Executable with zipapp Python Module

0
Convert Python file to exe

Application created with zipapp python module can run on the end user's system very easily. If you want to distribute your python code with all the dependencies in it. It can run on their system, but the end-user only needs to have the suitable python, installed on their system and the rest will be handled by the zipapp Module.

How to distribute Your python code Easily

Using the python app module can help you create a standalone python app, that you can distribute among your clients and the end-user. They do not need to install anything else manually, they just need to have python installed on their system and the rest will be in the zipapp module.

I have a hard time sending and giving my python code to the client. They were mainly from nontech backgrounds and they were often having a hard time installing the python program that i have created for them. You do not have to do the hustles.

Create Python Archive with requirements.txt

Follow the following steps to create a stand-alone python app with the requiremetns.txt file in it.

Step No 1:

Move all of your code into a directory and have one __main__.py file in it.

Step NO 2:

If you do not have all the dependencies installed in your app already then you should install them first.

Step No 3:

Now Package your application with the help of the following command using the zipapp python module.

$ python -m zipapp -p "interpreter" myappname

Congratulations, you now have a fully functional file of your code that you can send to anyone and they can run this code if they have just the appropriate interpreter installed on their system.

Create Exe file from your Python code

You can create an executable file from your python code using the zipapp module, which is a built-in python module.

from distutils.ccompiler import new_compiler
import distutils.sysconfig
import sys
import os
from pathlib import Path

def compile(src):
    src = Path(src)
    cc = new_compiler()
    exe = src.stem
    cc.add_include_dir(distutils.sysconfig.get_python_inc())
    cc.add_library_dir(os.path.join(sys.base_exec_prefix, 'libs'))
    # First the CLI executable
    objs = cc.compile([str(src)])
    cc.link_executable(objs, exe)
    # Now the GUI executable
    cc.define_macro('WINDOWS')
    objs = cc.compile([str(src)])
    cc.link_executable(objs, exe + 'w')

if __name__ == "__main__":
    compile("zastub.c")

introduction to the zipapp Python Module

The zipapp python module is used to manage the executable python zip archives. With the help of this python module, we can create the zip archive that contains the python code and we can execute these codes in our python program.

Create a Zip Archive of Python code

The create_archive() method of the zipapp module can be used to create the archive of the python files. The code that is added to the archive can be executed by the zipapp module. We can control the creation of the archive with the help of the parameter that should be passed to the method call.

See the following code
import  zipapp

zipapp.create_archive('Roughwork/rawtestingof.py','Roughwork/rawtestingof.pyz')
# Output The output is a .pyz file in the specified directory

Full Signature of the method create_archive()

create_archive(source, target=None, interpreter=None, main=None, filter=None, compressed=False)

Source: can be any directory, Path, or file object. In our case, it was a python file.
target: it can be the path where we want to save our archive.

Related Posts:

Post a Comment

0Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !