pyinstaller uses spec files to package usage templates

pyinstaller packaging

Using pyqt5 to develop software, when the project becomes larger and larger and more resources are referenced, it is difficult to meet the packaging requirements by using pyinstaller without using spec files.

spec file. In fact, you are using pyinstaller main Py is also generated automatically when it is packaged. It is called main spec.

However, if you want to package your own resource files, you need to edit the spec file and then use pyinstaller main Spec can be packaged.

Pyinstaller is a basic and conventional packaging method. There are a lot of articles on the Internet, and there are a lot of people searching by themselves. I won't list them one by one. Here is a recommended article that helped me the most when I first learned to use pyinstaller. At the same time, I would like to thank the great God author.

python packaging program for pyinstaller

This article mainly lists several usage templates that pyinstaller uses spec files to package for your reference. As for the connotation principle, I have limited time and have not studied it in depth. Let's explore it according to our own situation.

chachezulin

[the following code can be used after being completely copied and directly run]
[Note 1: the relevant path and file name in the template need to be modified according to your own situation]
[Note 2: if your spec file is called main.spec, the package command is pyinstaller main.spec]
[Note 3: when the project is getting larger and larger, the software startup speed of the green folder without installation is faster than that of a single executable! **]

Mode 1: use spec file to print [single executable file]

# -*- mode: python -*-

block_cipher = None


a = Analysis(['main.py'],
             pathex=['D:\\PythonProject\\mysoft'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
			 



#######!!!Note 1: load your own resource file#####################
def extra_datas(mydir):
    def rec_glob(p, files):
        import os
        import glob
        for d in glob.glob(p):
            if os.path.isfile(d):
                files.append(d)
            rec_glob("%s/*" % d, files)
    files = []
    rec_glob("%s/*" % mydir, files)
    extra_datas = []
    for f in files:
        extra_datas.append((f, f, 'DATA'))

    return extra_datas

# append the 'Resources' dir
a.datas += extra_datas('Resources')    ###Here is your own resource folder		
a.datas += extra_datas('Reports')	  ###Here is your own resource folder
a.datas += extra_datas('Drivers')	 ###Here is your own resource folder
################################################
			 
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)

			 
exe = EXE(pyz,
          a.scripts,
          a.binaries,                          ###!!! Note 2
          a.zipfiles,                          ###!!! Note 2
          a.datas,                             ###!!! Note 2
          [],
          exclude_binaries=False,   ###!!! Note 3: This is False
          name='mysoft',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=False,
          icon='d:\\mysoft.ico')

Mode 2: use spec file and print it as [installation free green folder]

# -*- mode: python -*-

block_cipher = None


a = Analysis(['main.py'],
             pathex=['D:\\PythonProject\\mysoft'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
			 



#######!!!Note 1: load your own resource file#####################
def extra_datas(mydir):
    def rec_glob(p, files):
        import os
        import glob
        for d in glob.glob(p):
            if os.path.isfile(d):
                files.append(d)
            rec_glob("%s/*" % d, files)
    files = []
    rec_glob("%s/*" % mydir, files)
    extra_datas = []
    for f in files:
        extra_datas.append((f, f, 'DATA'))

    return extra_datas

# append the 'Resources' dir
a.datas += extra_datas('Resources')	 ###Here is your own resource folder		
a.datas += extra_datas('Reports')	 ###Here is your own resource folder	
a.datas += extra_datas('Drivers')	 ###Here is your own resource folder	
################################################
			 
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)

			 
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,   ###!!! Note 3: here is True
          name='mysoft',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=False,
          icon='d:\\mysoft.ico')


coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='mysoft')

Mode 3: use the spec file and simultaneously print out [single executable] and [installation free green folder]

# -*- mode: python -*-

block_cipher = None


a = Analysis(['main.py'],
             pathex=['D:\\PythonProject\\mysoft'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
			 



#######!!!Note 1: load your own resource file#####################
def extra_datas(mydir):
    def rec_glob(p, files):
        import os
        import glob
        for d in glob.glob(p):
            if os.path.isfile(d):
                files.append(d)
            rec_glob("%s/*" % d, files)
    files = []
    rec_glob("%s/*" % mydir, files)
    extra_datas = []
    for f in files:
        extra_datas.append((f, f, 'DATA'))

    return extra_datas

# append the 'Resources' dir
a.datas += extra_datas('Resources')	 ###Here is your own resource folder	
a.datas += extra_datas('Reports')	 ###Here is your own resource folder	
a.datas += extra_datas('Drivers')	 ###Here is your own resource folder	
################################################
			 
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)


exe1 = EXE(pyz,
          a.scripts,
          a.binaries,                          ###!!! Note 2
          a.zipfiles,                          ###!!! Note 2
          a.datas,                             ###!!! Note 2
          [],
          exclude_binaries=False,   ###!!! Note 3: This is False
          name='mysoft',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=False,
          icon='d:\\mysoft.ico')

			 
exe2 = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,   ###!!! Note 3: here is True
          name='mysoft',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=False,
          icon='d:\\mysoft.ico')


coll = COLLECT(exe2,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='mysoft')
preview

If this article is helpful, please leave a message for encouragement.
If there is any error in this article, please leave a message for improvement.

Posted by harinath on Wed, 01 Jun 2022 02:50:59 +0530