1
votes

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?

3

3 Answers

0
votes

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.

4
votes

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

0
votes
  • 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.