How to compile multiple python files into single .exe file using pyinstaller
Solution 1
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 2
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
sar678
Updated on June 05, 2022Comments
-
sar678 almost 2 years
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?
-
Rohit Salunke almost 7 yearsYou can follow this steps:stackoverflow.com/a/37140662/5114581
-
-
sar678 almost 7 yearsThanks 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 almost 7 yearsThis works perfectly when python is installed in my computer. How can we run this application in computers where python is not installed?
-
DFE almost 7 yearspack your python_file.py script into a function (called python_file() for example) and call it from your main script instead of os.system.
-
sar678 almost 7 yearsHow 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
-
DFE almost 7 yearshiddenimports=[] is the key