Skip to content Skip to sidebar Skip to footer

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

Solution 1:

Suppose you have two files: "my_main.py" and "my_functions.py". Assume that "my_main.py" imports methods from "my_functions.py"

Execute the following command:

pyinstaller --onefile my_main.py my_functions.py

Resulting single executable file "my_main.exe" will be created under "dist" folder of present working directory.

Identical process for Linux/Windows. For more than two python files just include them one after separated by space.

pyinstaller --onefile my_main.py my_functions.py file3.py file4.py

Solution 2:

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.

Solution 3:

  • Not fully validated, maybe useful for your reference.

  • Background: I have use several time PyInstaller in Win and Mac.

  • Answer for your question:

    • pyinstaller --onefile tkinter_app.py python_file.py
  • Extra Explanation: If main.py import some_other.py (and/or some other libs, such as above someone mentioned numpy, pandas, etc ..., then PyInstaller can auto recognize it, do not need add --onefile and other py file, just need

    • pyinstaller main.py
  • You should refer official doc if expect detailed explanation.

Post a Comment for "How To Compile Multiple Python Files Into Single .exe File Using Pyinstaller"