Search code examples
pythonpython-2.7tkinterpyinstaller

How to compile multiple python files into single .exe file using pyinstaller


I have created a GUI (using Tkinter) in python and this runs python files on click of a button from GUI using os.system('python_file.py'). I wanted to bundle all these python files into single .exe file using pyinstaller by keeping the Tkinter file as main.

I created the .exe file by doing the following in command line:

pyinstaller --debug --onefile --noupx tkinter_app.py

Currently my .spec file looks like this:

# -*- mode: python -*-

block_cipher = None

a = Analysis(['tkinter_app.py'],pathex=['C:\\test'],binaries=[],datas=[],
hiddenimports=[],hookspath=[],runtime_hooks=[],excludes=[], win_no_prefer_redirects=False,
win_private_assemblies=False, cipher=block_cipher)

pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(pyz, a.scripts, a.binaries, a.zipfiles, a.datas, name='tkinter_app', debug=True, strip=False, upx=False,console=True )

I'm not sure how to include the other python files in the above .spec file so that the whole application works. Can someone please help?


Solution

  • The best way is to use the array datas

    For example like this:

    a = Analysis(['.\\main.py'],
                 pathex=['.'],
                 binaries=None,
                 datas=[ ('.\\Ressources\\i18n', 'i18n'),
                 ('.\\Ressources\\file1.py', '.')],
                 hiddenimports=[],
                 hookspath=[],
                 runtime_hooks=[],
                 excludes=[],
                 win_no_prefer_redirects=False,
                 win_private_assemblies=False,
                 cipher=block_cipher)
    

    Note: Make sure to put it in the right relative path so your program will be able to access it

    Edit: Given your error message, the problem is not in packaging with PyInstaller but in os.system command.

    os.system is equivalent to opening a DOS command window and typping your command python_file.py

    To access your python files, you have to know:

    • PyInstaller unpack your packed files in a temporary folder that you can access in sys._MEIPASS (only works from the .exe)
    • os.system can be used to launch python given the complete path to the file like this: os.system("python " + os.path.join(sys._MEIPASS, "python_file.py"))

      But be carefull, this will only work if python is installed on the system (and included in syspath) and from the exe. Executing directly your python file will send exception.