I'm trying to make an executable with PyInstaller's onefile option. I've tried many, many different ways of fixing it and I'm absolutely stuck. Specifically, I tried
Pyinstaller and --onefile: How to include an image in the exe file. I made my spec file look like theirs and included the resource_path() function. However, I get the following error:
File "PIL/Image.py", line 2953, in open
FileNotFoundError: [Errno 2] No such file or directory'/var/folders/t5/vkb5xkjs3517p5jlfkbj89vm0000gp/T/_MEIdOLb1O/logo.png'
So I'm not sure if my problem lies in the MEIPASS part, or maybe something with PIL. Another weird part of this is that before this error, my code includes:
self.iconbitmap(r"[workspace]/src/icon.ico")
Which produces no problem, even though both files (icon.ico, logo.png) are located in the same folder as the program that I turn into an executable. If it makes a difference, I'm running this on Mac.
The command I'm currently using:
pyinstaller --noconfirm --onefile --console "[workspace]/src/gui.py" gui.spec
and my spec file:
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['gui.py'],
pathex=['/Users/Freddie/Impruvon/guiwebscraperproject/venv/src'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
for d in a.datas:
if 'pyconfig' in d[0]:
a.datas.remove(d)
break
a.datas += [('logo.png','/Users/Freddie/Impruvon/guiwebscraperproject/venv/src/logo.png', 'Data')]
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='gui',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True,
disable_windowed_traceback=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None )
Okay well I was sure I had already tried this, but by adding the resource path method from Pyinstaller and --onefile: How to include an image in the exe file, but without changing the spec file, this command works:
pyinstaller --noconfirm --onefile --console --add-data "[workspace]/src/logo.png:." "[workspace]/src/gui.py"
Use this instead and then with pyinstaller convert main file to exe -
If this is the pixmap then add the function below and call it in pixmap
self.label.setPixmap(
QtGui.QPixmap(
self.resource_path("MyPNG_FILE.png")
)
)
Add this function to your code it will work for labels, images etc
### Use this function To attach files to the exe file (eg - png, txt, jpg etc) using pyinstaller
def resource_path(self, relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
base_path = sys._MEIPASS
else:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
Related
I have 2 files, One is Main.py other one is Autoe.ui, I want both as a single .exe
I tried
pyinstaller -w --add-data="Autoe.ui;." Main.py
This works just fine, but it creates lot of other files as well, I wanted just a single exe, So I tried this
pyinstaller.exe -w --onefile --add-data="Autoe.ui;." Main.py
This creates a single .exe but it won't run, I get a pop-up saying "Failed to execute script Main"
I had a similar problem with the Kivy app. In my case it was a kv file, not a ui. Maybe what I did will help you.
In the folder with the * .py file I ran the command:
pyinstaller --onefile -y --clean --windowed --icon=someicon.ico main.py
The main.spec file appeared in the folder. I have edited it.
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(["main.py"],
pathex=["C:\\Users\\underground\\Desktop\\gdc"], #<<<<<<<path to folder of your app
binaries=[]
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
a.datas += [('gdc.kv', 'C:\\Users\\underground\\Desktop\\gdc\\gdc.kv', 'DATA')] #<<<< I added kv file to a.datas
excluded_binaries = ['VCRUNTIME140.dll'] #<<<< I disabled this library because my app won't start on win10
a.binaries = TOC([x for x in a.binaries if x[0] not in excluded_binaries])
exe = EXE(pyz, Tree('C:\\Users\\underground\\Desktop\\gdc\\Data','Data'),
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='main',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=False,
upx_exclude=[],
runtime_tmpdir=None,
console=False , icon='ikona.ico')
When I edited the spec file, I ran pyintaller again but gave the spec file instead of the * .py file.
pyinstaller main.spec
...that's all.
You can also try the GUI version - auto-py-to-exe instead of pyinstaller.
I am trying to get Pyinstaller 3.5 to create a "onefile" executable but it keeps generating a "onedir" instead.
My files are all in one directory. There is a main program that imports two other modules which in turn import a third. The program functions properly when run directly in Python 3.7.4. The "onedir" version generated by Pyinstaller also works. I'm running 64 bit Windows 10 Pro on a Surface Book 2.
The command I'm using to generate the file is:
pyinstaller --onefile --windowed --additional-hooks-dir=. qualys_admin.spec
My program uses wx, pubsub, xmltodict, requests, and pandas. The additional hook file is for xlrd which pandas needs to read a xlsx file.
My spec file looks like this:
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
# work-around for https://github.com/pyinstaller/pyinstaller/issues/4064
import distutils
if distutils.distutils_path.endswith('__init__.py'):
distutils.distutils_path = os.path.dirname(distutils.distutils_path)
addedfiles = ('qualys_admin.ini', '.')
a = Analysis(['qualys_admin.py'],
pathex=['C:\\Users\\secops-sw\\Documents\\qualys-
administration\\qualysadmin'],
binaries=[],
datas=[addedfiles],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
[],
exclude_binaries=True,
name='qualys_admin',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='qualys_admin')
I wrote a tiny "hello world" program to take the complexity of my program off the table. I was able to generate both a "onedir" and a "onefile" successfuly. So I know where to look for the standalone executable.
For my qualys_admin app, that executable is definitely not being generated and I cannot find any warnings to indicate why.
Does anyone have any ideas?
It is not working because your spec file is configured for one directory mode. You need to create the spec file with the one file flag.
pyi-makespec --onefile yourscript.py
Then you can modify your spec sheet and build the app using the custom spec sheet.
I’m New to Python Coding and just finished my first python scripted
I’m trying to publish my programme so that I can install in on another device.
But as soon as I convert it from .py to .exe with pyinstaller and try to run my programme it gives me the error:
fatal error: failed to execute scrip
Code I used in to convert:
pyinstaller -w file_name.py
pyinstaller -F file_name.py
pyinstaller -i "c:\\icon_file path" file_name.py
am I just missing as step or is there something else I can try to resolve this problem?
I usually code on Visual studio and when I test run everything worked fine.
My .spec file:
block_cipher = None
a = Analysis(['file_name.py'],
pathex=['C:\\Users\\MainUser\\Desktop\\Publishing'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
a.binaries = a.binaries +
[('libsha1.dll','/home/iot/lib/libsha1.dll','BINARY')]
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='file_name',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
runtime_tmpdir=None,
console=True )
```
Usually, this is due to a lack of file when packaging.
When you use PyInstaller, you can use it like this:
python -m PyInstaller .\yourFile.py
then, a yourFile.spec file is generated under this folder.
you should edit this file, add all project file into datas,
a = Analysis(['yourFile.py'],
pathex=['D:\\projectPath\\project'],
binaries=[],
datas=[('D:\\projectPath\\project\\*.py', '.'),
('D:\\projectPath\\project\\UI\\*.ui', 'UI'),
('D:\\projectPath\\project\\other\\*.py', 'other'),
],
...
)
It's simulated up here, a project that contains the UI and other folders. It like a tuple, ('full path', 'folder name').
If you have *.dll on Windows or *.so on Linux, you must be write into binaries:
a.binaries = a.binaries + [('libsha1.so','/home/iot/lib/libsha1.so','BINARY')]
I am guessing you only have one script, so if you use:
Pyinstaller --onefile yourScript.py
Replacing yourScript.py with the name of your python file in the CMD/Terminal, you shouldn't have any problems.
If you are missing a binary, this should help. For example pyinstaller was missing the currency converter module, so I found and it, got the zip file and then ran this in CMD:
Pyinstaller --add-binary "C:\Users\myName\Downloads\eurofxref-hist.zip";currency_converter --onefile myScript.py
Where myScript.py is my Python script, and the link is to the folder with the binary zip file.
I am trying to convert my code into an exe using pyinstaller spec.
I ran pyinstaller with the following command:
pyinstaller --clean --add-data lib_lightgbm.dll;\compile orca.spec
The exe fails with the error:
main__.PyInstallerImportError: Failed to load dynlib/dll
'C:\\Users\\...\\lightgbm\\../compile\\lib_lightgbm.dll'. Most probably this
dynlib/dll was not found when the application was frozen.
I have tried adding lightgbm.dll through binaries, but didnt work. I also tried manually copying it to the destination after the exe is created. That didnt work either. Most of the questions about pyinstaller and lib_lightgbm.dll failed to answer my issue. Can someone please suggest a solution? I am at my wits end at the moment.
Here is my spec file:
# -*- mode: python -*-
import sys
sys.setrecursionlimit(5000)
block_cipher = None
a = Analysis(['mycode.py'],
pathex=['C:\\mycode\\source code'],
binaries=[],
datas=[],
hiddenimports=['cython', 'sklearn', 'sklearn.feature_extraction','sklearn.pipeline', 'sklearn.ensemble', 'sklearn.neighbors.typedefs', 'sklearn.neighbors.quad_tree', 'sklearn.tree._utils'],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
[],
exclude_binaries=True,
name='mycode',
debug=True,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='mycode')
When you build from a spec file, those options cannot be changed.
so either add the tuple to the datas section, or ommit the spec file and use all the parameters for pyinstaller
I'm not 100% sure this is the fix, but there should be something in your datas section of you spec file. So I'd investigate that. you can also build from a spec file using pyinstaller <spec file here> when you're done editing it.
https://pyinstaller.readthedocs.io/en/stable/spec-files.html
After updating the Pyinstaller spec file as suggested in the answer here (How to include chromedriver with pyinstaller?), chromedriver is still not being accessed from the generated app file. Could the issue be with .\\selenium\\webdriver? That was copied from the answer and I'm not sure it's specific to a Windows OS.
Running the UNIX executable in terminal works, accessing chromedriver.
The full spec file is:
# -*- mode: python -*-
block_cipher = None
a = Analysis([‘scriptname.py'],
pathex=['/Users/Name/Desktop'],
binaries=[('/usr/local/bin/chromedriver', '.\\selenium\\webdriver')],
datas=None,
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=‘app name’,
debug=False,
strip=False,
upx=True,
console=False )
app = BUNDLE(exe,
name=‘appname.app',
icon=None,
bundle_identifier=None)
The line pyinstaller appname.spec scriptname.py --windowed --onefile is used in terminal to generate the app.
Yes, that was Windows path. In Unix, you need to use ./selenium/webdriver instead. It tells where to place the chromedriver binary in the bundle, so after pyinstall, chromedriver will be in /path/to/bundle/dist/selenium/webdriver.
Then in the code you should use something like this to reach it (it's a remote example):
dir = os.path.dirname(__file__)
chrome_path = os.path.join(dir, 'selenium','webdriver','chromedriver.exe')
service = service.Service(chrome_path) ...