I'm trying to build a one-file EXE with PyInstaller.
I'm importing it with Ui_MainWindow, QtBaseClass = uic.loadUiType("first_qt.ui")
code inside main.py file.
pyinstaller --name="MyApplication" --onefile main.py
Code output:
24 INFO: PyInstaller: 3.5
24 INFO: Python: 3.7.5
24 INFO: Platform: Linux-5.0.0-36-generic-x86_64-with-Ubuntu-18.04-bionic
25 INFO: wrote /home/you/Installer/MyApplication.spec
27 INFO: UPX is not available.
28 INFO: Extending PYTHONPATH with paths
['/home/you/Installer', '/home/you/Installer']
28 INFO: checking Analysis
28 INFO: Building Analysis because Analysis-00.toc is non existent
28 INFO: Initializing module dependency graph...
30 INFO: Initializing module graph hooks...
31 INFO: Analyzing base_library.zip ...
1719 INFO: running Analysis Analysis-00.toc
1741 INFO: Caching module hooks...
1744 INFO: Analyzing /home/you/Installer/main.py
1831 INFO: Processing pre-safe import module hook urllib3.packages.six.moves
2483 INFO: Processing pre-find module path hook PyQt5.uic.port_v3
2486 INFO: Processing pre-find module path hook PyQt5.uic.port_v2
2577 INFO: Loading module hooks...
2577 INFO: Loading module hook "hook-xml.py"...
2617 INFO: Loading module hook "hook-PyQt5.QtGui.py"...
2694 INFO: Loading module hook "hook-PyQt5.QtWidgets.py"...
2737 INFO: Loading module hook "hook-PyQt5.uic.py"...
2738 INFO: Loading module hook "hook-certifi.py"...
2739 INFO: Loading module hook "hook-PyQt5.QtCore.py"...
2759 INFO: Loading module hook "hook-PyQt5.py"...
2771 WARNING: Hidden import "sip" not found!
2771 INFO: Loading module hook "hook-xml.etree.cElementTree.py"...
2772 INFO: Loading module hook "hook-encodings.py"...
2818 INFO: Loading module hook "hook-pydoc.py"...
2831 INFO: Looking for ctypes DLLs
2831 INFO: Analyzing run-time hooks ...
2834 INFO: Including run-time hook 'pyi_rth_pyqt5.py'
2836 INFO: Including run-time hook 'pyi_rth_certifi.py'
2843 INFO: Looking for dynamic libraries
5002 INFO: Looking for eggs
5002 INFO: Python library not in binary dependencies. Doing additional searching...
5058 INFO: Using Python library /usr/lib/x86_64-linux-gnu/libpython3.7m.so.1.0
5062 INFO: Warnings written to /home/you/Installer/build/MyApplication/warn-MyApplication.txt
5095 INFO: Graph cross-reference written to /home/you/Installer/build/MyApplication/xref-MyApplication.html
5105 INFO: checking PYZ
5105 INFO: Building PYZ because PYZ-00.toc is non existent
5105 INFO: Building PYZ (ZlibArchive) /home/you/Installer/build/MyApplication/PYZ-00.pyz
5406 INFO: Building PYZ (ZlibArchive) /home/you/Installer/build/MyApplication/PYZ-00.pyz completed successfully.
5411 INFO: checking PKG
5412 INFO: Building PKG because PKG-00.toc is non existent
5412 INFO: Building PKG (CArchive) PKG-00.pkg
28572 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
28576 INFO: Bootloader /home/you/.local/share/virtualenvs/Installer-p1t8sryV/lib/python3.7/site-packages/PyInstaller/bootloader/Linux-64bit/run
28577 INFO: checking EXE
28577 INFO: Building EXE because EXE-00.toc is non existent
28577 INFO: Building EXE from EXE-00.toc
28577 INFO: Appending archive to ELF section in EXE /home/you/Installer/dist/MyApplication
28979 INFO: Building EXE from EXE-00.toc completed successfully.
When I run the application I get the following error:
File "main.py", line 18, in <module>
File "site-packages/PyQt5/uic/__init__.py", line 200, in loadUiType
File "site-packages/PyQt5/uic/Compiler/compiler.py", line 111, in compileUi
File "site-packages/PyQt5/uic/uiparser.py", line 1020, in parse
File "xml/etree/ElementTree.py", line 1197, in parse
File "xml/etree/ElementTree.py", line 587, in parse
FileNotFoundError: [Errno 2] No such file or directory: 'first_qt.ui'
[23863] Failed to execute script main
I tried to convert it to first.py file with pyuic5 and try failed. Is there another way?
Similar topics: PyInstaller + UI Files - FileNotFoundError: [Errno 2] No such file or directory:
Edit:
After executing the pyuic5 first_qt.ui > output.py
command, I added import output to main.py
:
...
import output
#Ui_MainWindow, QtBaseClass = uic.loadUiType("first_qt.ui")
# Import .ui forms for the GUI using function resource_path()
form = resource_path("first_qt.ui")
Ui_MainWindow, QtBaseClass = uic.loadUiType(form)
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
...
I got an error output like this:
Traceback (most recent call last):
File "main.py", line 22, in <module>
NameError: name 'resource_path' is not defined
[7217] Failed to execute script main
I got rid of the above error. Now I've encountered an error like this:
Traceback (most recent call last):
File "main.py", line 34, in <module>
File "site-packages/PyQt5/uic/__init__.py", line 200, in loadUiType
File "site-packages/PyQt5/uic/Compiler/compiler.py", line 111, in compileUi
File "site-packages/PyQt5/uic/uiparser.py", line 1020, in parse
File "xml/etree/ElementTree.py", line 1197, in parse
File "xml/etree/ElementTree.py", line 587, in parse
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/_MEID22LkJ/first_qt.ui'
[9606] Failed to execute script main
Edit2: MyApplication.spec output:
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['main.py'],
pathex=['/home/Installer'],
binaries=[],
datas=[("/home/Installer/first_qt.ui", ".")],
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,
a.binaries,
a.zipfiles,
a.datas,
[],
name='MyApplication',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True )
Then I ran the pyinstaller MyApplication.spec
command.
Problem solved.