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?

share|improve this question
up vote 0 down vote accepted

The best way is to use the array datas

For example like this:

a = Analysis(['.\\main.py'],
             datas=[ ('.\\Ressources\\i18n', 'i18n'),
             ('.\\Ressources\\file1.py', '.')],

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.

share|improve this answer
  • Thanks for your answer. I'm still not able to run the supporting python files from GUI by making the changes suggested. I get a error like 'file1.py is not recognized as an internal or external command, operable program or batch file'. SPEC FILE: a = Analysis(['C:\\Python27\\Scripts\\tkinter_app.py'], pathex=['.'], binaries=None, datas=[('C:\\Python27\\Tools\\i18n','i18n'), ('C:\\Python27\\Scripts\\file1.py','.'), ('C:\\Python27\\Scripts\\file2.py','.'), ('C:\\Python27\\Scripts\\file3.py','.'), ('C:\\Python27\\Scripts\\file4.py','.'), ('C:\\Python27\\Scripts\\file5.py','.'), ..... – sar678 Jul 21 '17 at 9:28
  • This works perfectly when python is installed in my computer. How can we run this application in computers where python is not installed? – sar678 Jul 26 '17 at 11:42
  • pack your python_file.py script into a function (called python_file() for example) and call it from your main script instead of os.system. – DFE Jul 27 '17 at 8:56
  • How do I include python libraries (numpy, pandas) when compiling using pyinstaller. As i'm running supporting python files using os.system, the libraries in these files are not being included in the exe file – sar678 Aug 3 '17 at 13:30
  • hiddenimports=[] is the key – DFE Aug 4 '17 at 14:18