I\'m trying to build a one-file EXE with PyInstaller which is to include an image and an icon. I cannot for the life of me get it to work with --onefile
.
Perhaps i missed a step or did something wrong but the methods which are above, didn't bundle data files with PyInstaller into one exe file. Let me share the steps what i have done.
step:Write one of the above methods into your py file with importing sys and os modules. I tried both of them. The last one is:
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, relative_path)
step: Write, pyi-makespec file.py, to the console, to create a file.spec file.
step: Open, file.spec with Notepad++ to add the data files like below:
a = Analysis(['C:\\Users\\TCK\\Desktop\\Projeler\\Converter-GUI.py'],
pathex=['C:\\Users\\TCK\\Desktop\\Projeler'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
#Add the file like the below example
a.datas += [('Converter-GUI.ico', 'C:\\Users\\TCK\\Desktop\\Projeler\\Converter-GUI.ico', 'DATA')]
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='Converter-GUI',
debug=False,
strip=False,
upx=True,
#Turn the console option False if you don't want to see the console while executing the program.
console=False,
#Add an icon to the program.
icon='C:\\Users\\TCK\\Desktop\\Projeler\\Converter-GUI.ico')
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='Converter-GUI')
step: I followed the above steps, then saved the spec file. At last opened the console and write, pyinstaller file.spec (in my case, file=Converter-GUI).
Conclusion: There's still more than one file in the dist folder.
Note: I'm using Python 3.5.
EDIT: Finally it works with Jonathan Reinhart's method.
step: Add the below codes to your python file with importing sys and os.
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, relative_path)
step: Call the above function with adding the path of your file:
image_path = resource_path("Converter-GUI.ico")
step: Write the above variable that call the function to where your codes need the path. In my case it's:
self.window.iconbitmap(image_path)
step: Open the console in the same directory of your python file, write the codes like below:
pyinstaller --onefile your_file.py
step: Save and exit the path file. Go to your folder which include the spec and py file. Open again the console window and type the below command:
pyinstaller your_file.spec
After the 6. step your one file is ready to use.