SoFunction
Updated on 2024-11-19

pyinstaller makes exes of python programs containing multiple py files.

1. Try to put the resource folder under the main folder


2.pyi-makespec

Production of spec files

file in the current folder.

Editing the spec file

# -*- mode: python -*-
block_cipher = None
 
a = Analysis(['', '',  
'E:\\py_project\\test_wpf_python_msg\\src\\recognition_module\\caption_recognizer.py', 
'E:\\py_project\\test_wpf_python_msg\\src\\recognition_module\\image_recognizer.py',
'E:\\py_project\\test_wpf_python_msg\\src\\recognition_module\\image_vectorizer.py',
'E:\\py_project\\test_wpf_python_msg\\src\\recognition_module\\',
'E:\\py_project\\test_wpf_python_msg\\src\\recognition_module\\test_fun.py',
'E:\\py_project\\test_wpf_python_msg\\src\\recognition_module\\unicom_area.py',
'E:\\py_project\\test_wpf_python_msg\\src\\recognition_module\\unicom_area_finder.py',
'E:\\py_project\\test_wpf_python_msg\\src\\recognition_module\\unicom_area_include_caption_judger.py',
'E:\\py_project\\test_wpf_python_msg\\src\\recognition_module\\value_vectorizer.py'],
             pathex=['E:\\py_project\\test_wpf_python_msg'],
             binaries=[],
             datas=[('E:\\py_project\\test_wpf_python_msg\\images', 'images'),
			('E:\\py_project\\test_wpf_python_msg\\network_parameters', 'network_parameters')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          ,
          exclude_binaries=True,
          name='main',
          debug=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               ,
               ,
               ,
               strip=False,
               upx=True,
               name='main')


(1) Write all the py files in the first list, if they are in a folder with main, you can write the file name directly, if they are not in a folder, you need to write the full path of the file.

I've tried adding paths to other files in pathex, then other py files without full paths, just the filename, but it failed.

(2) The element in datas is of type tuple, and the first argument to tuple is the path to the data file (not the py file) in the python project.

The second parameter is the name of the folder where the resource is stored in the exe, which should be the same as the folder name in the project.

(3) The most important thing is that the exe is in the outermost folder. Because it is the entry file, the current path of other py files that are called is the current path of the file. So in the source code must be put into the outermost project folder, so that the current path and the path to maintain consistency, in order to let the current path of other py files remain unchanged, they can find the data resource.

4. Make an exe with the previously configured spec file:pyinstaller -d

The exe made with -d is much faster than with -f, which packages all the dll files into one exe.

5. The generated exe is in the dist folder

to this article about pyinstaller will contain multiple py files python program into an exe article is introduced to this, more related pyinstaller py packaged into an exe content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!