'Pyinstaller > standalone Win .exe, creates dozens of files and folders: which ones to keep in distribution, and how to include my other .py files?

I'm creating a standalone Windows .exe with Pyinstaller. Its created a lot of files and folders in its dist directory. When I am distributing the .exe, I gather I have to distribute the DLLs and the PYD files, but what about the folders kivy, kivymd, certifi\ etc.?

Also, I have included in my main python file (called navdraw.py) a handful of my own modules I have written: MFile_Funcs.py, MString_Funcs.py, MColor_Functions.py. I don't want the user to be able to see these.
How do I include them in the standalone .exe ? (I tried --add-data to each one but when I ran the .exe it still asked for those individual .py files to be present in the current dir before it would run.)

Files created by Pyinstaller in dist\

 <DIR>          altgraph-0.17.2.dist-info
         12,304 api-ms-win-core-console-l1-1-0.dll
         11,576 api-ms-win-core-datetime-l1-1-0.dll
         11,576 api-ms-win-core-debug-l1-1-0.dll
         11,576 api-ms-win-core-errorhandling-l1-1-0.dll
         15,376 api-ms-win-core-file-l1-1-0.dll
         11,576 api-ms-win-core-file-l1-2-0.dll
         11,792 api-ms-win-core-file-l2-1-0.dll
         11,576 api-ms-win-core-handle-l1-1-0.dll
         12,088 api-ms-win-core-heap-l1-1-0.dll
         11,792 api-ms-win-core-interlocked-l1-1-0.dll
         12,600 api-ms-win-core-libraryloader-l1-1-0.dll
         14,136 api-ms-win-core-localization-l1-2-0.dll
         12,088 api-ms-win-core-memory-l1-1-0.dll
         11,792 api-ms-win-core-namedpipe-l1-1-0.dll
         12,816 api-ms-win-core-processenvironment-l1-1-0.dll
         14,136 api-ms-win-core-processthreads-l1-1-0.dll
         12,304 api-ms-win-core-processthreads-l1-1-1.dll
         11,280 api-ms-win-core-profile-l1-1-0.dll
         12,304 api-ms-win-core-rtlsupport-l1-1-0.dll
         11,576 api-ms-win-core-string-l1-1-0.dll
         13,624 api-ms-win-core-synch-l1-1-0.dll
         12,088 api-ms-win-core-synch-l1-2-0.dll
         12,808 api-ms-win-core-sysinfo-l1-1-0.dll
         12,088 api-ms-win-core-timezone-l1-1-0.dll
         11,784 api-ms-win-core-util-l1-1-0.dll
         12,600 api-ms-win-crt-conio-l1-1-0.dll
         15,672 api-ms-win-crt-convert-l1-1-0.dll
         12,088 api-ms-win-crt-environment-l1-1-0.dll
         13,840 api-ms-win-crt-filesystem-l1-1-0.dll
         12,600 api-ms-win-crt-heap-l1-1-0.dll
         12,088 api-ms-win-crt-locale-l1-1-0.dll
         21,008 api-ms-win-crt-math-l1-1-0.dll
         19,768 api-ms-win-crt-multibyte-l1-1-0.dll
         12,816 api-ms-win-crt-process-l1-1-0.dll
         16,400 api-ms-win-crt-runtime-l1-1-0.dll
         17,960 api-ms-win-crt-stdio-l1-1-0.dll
         18,472 api-ms-win-crt-string-l1-1-0.dll
         14,136 api-ms-win-crt-time-l1-1-0.dll
         12,296 api-ms-win-crt-utility-l1-1-0.dll
        800,520 base_library.zip
 <DIR>          build
 <DIR>          certifi
            173 color_theme.py
 <DIR>          dist
 <DIR>          docutils
          1,743 drivepath.py
          1,155 dropdown.py
        464,896 glew32.dll
 <DIR>          kivy
 <DIR>          kivymd
 <DIR>          kivy_install
      3,429,624 libcrypto-1_1.dll
         32,792 libffi-7.dll
        441,344 libFLAC-8.dll
        586,240 libfreetype-6.dll
        244,224 libjpeg-9.dll
        252,928 libmodplug-1.dll
        337,408 libmpg123-0.dll
         52,224 libogg-0.dll
        124,928 libopus-0.dll
         46,592 libopusfile-0.dll
        210,944 libpng16-16.dll
        695,032 libssl-1_1.dll
        432,640 libtiff-5.dll
        251,904 libvorbis-0.dll
         69,632 libvorbisfile-3.dll
        447,488 libwebp-7.dll
          1,656 LICENSE.FLAC.txt
          7,042 LICENSE.freetype.txt
          3,155 LICENSE.jpeg.txt
            193 LICENSE.modplug.txt
         41,624 LICENSE.mpg123.txt
          1,632 LICENSE.ogg-vorbis.txt
          2,107 LICENSE.opus.txt
          1,649 LICENSE.opusfile.txt
          5,611 LICENSE.png.txt
          1,301 LICENSE.tiff.txt
          1,660 LICENSE.webp.txt
          1,696 LICENSE.zlib.txt
            591 MColor_Functions.py
      6,065,952 mfc140u.dll
          3,053 MFile_Funcs.py
          4,353 MString_Funcs.py
        627,992 MSVCP140.dll
 <DIR>          PIL
        203,496 pyexpat.pyd
 <DIR>          pyinstaller-4.10.dist-info
      4,490,472 python39.dll
        556,544 pythoncom39.dll
        142,336 pywintypes39.dll
      1,471,488 SDL2.dll
        125,440 SDL2_image.dll
        123,904 SDL2_mixer.dll
         33,792 SDL2_ttf.dll
         28,904 select.pyd
      1,044,880 ucrtbase.dll
      1,121,512 unicodedata.pyd
         97,168 VCRUNTIME140.dll
        134,656 win32api.pyd
 <DIR>          win32com
        157,184 win32file.pyd
        228,864 win32gui.pyd
         22,528 win32trace.pyd
      1,428,480 win32ui.pyd
        108,544 zlib1.dll
         65,256 _asyncio.pyd
         86,760 _bz2.pyd
        126,696 _ctypes.pyd
        272,104 _decimal.pyd
        188,648 _elementtree.pyd
         66,280 _hashlib.pyd
        163,048 _lzma.pyd
         30,440 _multiprocessing.pyd
         46,312 _overlapped.pyd
         29,416 _queue.pyd
         80,104 _socket.pyd
        155,368 _ssl.pyd
         23,784 _uuid.pyd
         12,288 _win32sysloader.pyd
 <DIR>          __pycache__

My current navdraw.spec file:

from kivy_deps import sdl2, glew
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['navdraw.py'],
             pathex=[],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             hooksconfig={},
             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)

a.datas += [('Code\navdraw.kv',
'C:\\kivymd\\exe\navdraw.kv', 'DATA')]  


exe = EXE(pyz,
          a.scripts, 
          [],
          exclude_binaries=True,
          name='navdraw',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=False,
          disable_windowed_traceback=False,
          target_arch=None,
          codesign_identity=None,
          entitlements_file=None )
coll = COLLECT(exe,
Tree('C:\\kivymd\\exe\\'),
               a.binaries,
               a.zipfiles,
               a.datas, 
               *[Tree(p) for p in
               (sdl2.dep_bins +
               glew.dep_bins)],
               strip=False,
               upx=True,
               upx_exclude=[],
               name='navdraw')

Pyinstaller command line I tried: pyinstaller --add-data "MFile_Funcs.py;_Functions.py;." --add-data "MString_Funcs.py;." --add-data "drivepath.py;." navdraw.spec -y -F



Solution 1:[1]

By default, Pyinstaller creates a one folder distributable, and that is what your spec file does. Also when you specify a spec file on the Pyinstaller command line, most other command line options are ignored, from the documentation:

Only the following command-line options have an effect when building from a spec file:

--upx-dir

--distpath

--workpath

--noconfirm

--ascii

--clean

so your --add-data command line options will have no effect. You can build a single file executable by removing your navdraw.spec file and run:

pyi-makespec --onefile navdraw.py

Then you can edit navdraw.spec to add any missing files (any files referenced by an import should be included automatically). Then just run:

pyinstaller navdraw.spec

The resulting exe file in the dist folder should be a stand alone executable.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 John Anderson