Sunday, December 16, 2018

codingdirectional: Create a python application with pyinstaller and inno setup

The new python application (Multitas) which I have developed recently has basically completed, I will continue to include more features into this application in the future but before I further develop the application I would really like to share the latest features of this python application for windows os with you all so I have decided to upload the revised version of this application to Github to share it with you all. In this article, we will look at the process of packaging the new python application with the help of pyinstaller and inno setup before uploading it to Github.

Before you can use the above-mentioned tools to pack up any python application for windows os, you will need to install them first on your pc. Installing pyinstaller on the pc is a straightforward process, all you need to do is to open up the windows command prompt then type in below line to download and then install this tool on your pc. Before you can use pyinstaller, you must also have the latest version of python installed on the pc first.

pip install pyinstaller

The next step we need to do is to use this tool to create a distribution folder which contains our python application. In windows command prompt, change the directory to the location where you keep all the python files and the main python file of your new application. Then type in this line to generate a dist and the build folder in that same location.

Generate the dist and build folder with pyinstaller

What we did here is to create a windows’ python application (-w switch) with one directory feature (-D switch) which will then create two folders, dist and build folder where the python application and all the required files will be put inside the dist folder which will be shipped to the user later on. We can also specify the icon file (icon.ico) to be used as the icon for the application with the (-i) switch followed by the path to the icon file in your hard drive. The final part of the command is the path to the main application file which is the one which will start the entire application. You do not need to worry about the other related files that the main application file needs, pyinstaller will take care of that part for you. However, pyinstaller will not pack up the image or the other media type of files so you will need to edit the spec later on to include the extra media files used by the application program. After the dist folder and the build folder have been generated by pyinstaller, it is time to edit the spec file located in the same location as the above folders, the name of the spec file is also the same as the name of the main application file, open up the spec file to edit the datas variable within the spec, here is the entire source code.

# -*- mode: python -*-

block_cipher = None


a = Analysis(['Multitas.py'],
             pathex=['E:\\pythonprojects\\Multitas'],
             binaries=[],
             datas=[('icon.ico', '.')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='Multitas',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=False , icon='icon.ico')
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='Multitas')

The dot means to put the image file to the same folder as the main python file. The last step we need to do is to recompile the spec file, kindly take note again that the name of the spec file is the same as the main python file, we will run this command on the command prompt.

Recompile the spec file 

That is basically it for the pyinstaller part, you can now directly upload the dist folder to a file hosting site or email it to your friend in a zip format if you want to. However, as you can see from below it is very hard to find the application file if your friend is not a computer expert. Thus we will need to create a one click python application for our windows os laptop. Lets create it with inno setup. Follow this link to download inno setup if you do not have it installed in your computer.

All files inside the dist folder

After you have installed inno setup it is time to start it up,

1) Select create a new script file using the script wizard then pressed OK
2) Press next then fill in the application information then press next again
3) Press Next
4) Select the application file creates by pyinstaller then the folder which contains all the required python files
5) Click next after you have ticked the require fields
6) Provide the license file and the information files location if you have any then click on next
7) Select the set up languages
8) Specified the output folder, the name of the setup file, the icon of the setup file, if you are planning to sell the application for money then create a password
9) Click next
10) Click finish then follow a few pop up steps require by the script to create the setup file
The setup file is ready

That is it, now you can upload this single file to the online file hosting site or emailed it to your friend so he can start to install the application on his windows os desktop! With that we have concluded the python application project tutorial. I might put one more source code for the new feature of this application online then will continue to develop this application and upload the latest version of this application to github once it is ready. If you are a windows user then don’t forget to download the setup file then install that application on your windows os computer!



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...