Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyinstaller - Include empty folder in dist

I'm trying to build a python script that needs a specific directory to create some files into. When I try to build the application using "PyInstaller" it doesn't include this empty "reports" folder in the dist. All the other folders that have files in them are included as normal. Note that I'm not using the --onefile command and the executable itself is located inside the folder: dist/main/main.exe

My main.spec file

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None

added_files = [
         ( 'database/reports.sqlite', 'database' ),
         ( 'templates/*', 'templates' ),
         ( 'reports', 'reports' )
         ]

a = Analysis(['main.py'],
             pathex=['\\\\SRV\\Data\\pyi'],
             binaries=[],
             datas=added_files,
             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='main',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=False )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               upx_exclude=[],
               name='main')
like image 294
sarotnem Avatar asked Feb 01 '26 18:02

sarotnem


1 Answers

I had the same issue as yours and I managed to "copy" a folder to the output using the --add-data option provided by PyInstaller.

Note, I tested this command on a Windows 10 machine using the version 5.7.0 of PyInstaller:

py -m PyInstaller --add-data "Logs;Logs" .\main.py

On Linux or other OS this should work:

pyinstaller --add-data "Logs:Logs" ./main.py

Where "Logs" was a folder in the root of the application, where the "main.py" file was located in.

As stated in the documentation, the option value should be in the form "SOURCE;DESTINATION" on Windows and "SOURCE:DESTINATION" on most Unix systems. The source could be both a file (e.g. example.json) or a folder. The destination is the realtive path in the "dist" folder where to copy the source ("." to put a file in the root of the dist folder).

Hope this helps someone else! :)

like image 162
Fabs xD Avatar answered Feb 03 '26 07:02

Fabs xD