Problems packaging PyQt application (including QML) using cx_freeze - python

I'm attempting to package a PyQt application using cx_freeze. I'm running Python 3.4, Qt 5.6, PyQt 5.5.1 and Cx_freeze 4.3.4 on Windows 7.
There are three pieces to my application, the python/PyQt code, a Qt UI file which contains the GUI elements, and a QML file which runs an interactive map similar to the places_map.qml example. The QML file using the osm map plugin.
My Qt UI file includes a QQuickWidget whose source is the QML file. I'm attempting to package the application, so that others can run it without installing python and Qt. I've created a custom setup.py scripts for cx_freeze to use. However, I keep encountering various ImportErrors related to the QQuickWidget when running my built Exe files. The error occurs when my Python code loads the UI file. I've attempted to include pretty much anything related to QQuickWidgets and the QML code that I can think of in my setup.py file. Initially, the ImportError was missing QQuickWidgets. After adding the SIP QQuickWidget files, the error is now related to QQuickWidgets.QQuickWidget. Below is my setup.py file.
from cx_Freeze import setup, Executable
import os
PYQT5_DIR = "c:/Python34/lib/site-packages/PyQt5/"
include_files = ['TTRMS.ui','places_map.qml',
(os.path.join(PYQT5_DIR, "qml", "QtQuick.2"), "QtQuick.2"),
(os.path.join(PYQT5_DIR, "qml", "QtQuick"), "QtQuick"),
(os.path.join(PYQT5_DIR, "qml", "QtQml"), "QtQml"),
(os.path.join(PYQT5_DIR, "qml", "Qt"), "Qt"),
(os.path.join(PYQT5_DIR, "qml", "QtPositioning"), "QtPositioning"),
(os.path.join(PYQT5_DIR, "qml", "QtLocation"), "QtLocation"),'C:/Python34/Lib/site-packages/PyQt5/uic/widget-plugins',
'C:/Python34/Lib/site-packages/PyQt5/plugins/geoservices','C:/Python34/Lib/site-packages/PyQt5/sip/PyQt5/QtQuickWidgets',
'C:/Python34/Lib/site-packages/PyQt5/sip/PyQt5/QtQuick']
buildOptions = dict(packages = ['PyQt5.QtQuickWidgets',"atexit","sip","PyQt5.QtCore","PyQt5.QtGui","PyQt5.QtWidgets",
"PyQt5.QtNetwork","PyQt5.QtOpenGL", "PyQt5.QtQml", "PyQt5.QtQuick"],
excludes = [], includes = ["atexit","re"], include_files = include_files)
import sys
base = 'Win32GUI' if sys.platform=='win32' else None
executables = [
Executable('Main.py', base=base, targetName = 'main.exe')
]
setup(name='TTRMS',
version = '1.0',
description = 'Travel Time Reliability',
options = dict(build_exe = buildOptions),
executables = executables)
Here is the error when running my Exe:
I'm not sure what else I need to include in my setup.py in order to package the application properly. I've successfully packaged a small PyQt app with a UI file, but never a UI file which then imports QML through a QQuickWidget. I'm not very knowledgeable about Qt either, so this whole process is new to me. Please let me know if you have any suggestions and let me know if I need to clarify anything. I appreciate any help!

Related

Application fails to start after building using cx_Freeze on OSX

I'm trying to build a simple app on OSX using cx_Freeze. The build using setup.py seems to go fine - it builds a .dmg and .app for my application, along with all of the source files. However, when I try to run the .app, it crashes immediately, saying "My_App quit unexpectedly". Frustratingly, I don't see any decipherable error codes I can track down.
I have stripped down the app into a very simple example. Below are my main python file and my setup file.
my_app.py
from tkinter import *
root = Tk()
root.title("Welcome to My_App")
root.geometry('350x200')
root.mainloop()
setup.py
import sys
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": [], "excludes": []}
# base="Win32GUI" should be used only for Windows GUI app
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup(
name = "My_GUI",
version = "1.0",
description = "My GUI application!",
options = {"build_exe": build_exe_options},
executables = [Executable("my_app.py", base=base)]
)
I have built an .exe on windows before, and when startup fails on windows, I at least get a traceback error message that I can track down. On OSX, it doesn't appear to do the same thing.
What's going wrong? How can I get more information on why my app is failing to start up? Below are images of my build and the error I'm getting on startup.
Ok turns out if I run the file from terminal, it outputs an actual error code. From there, I was able to track down that I did not have zlib installed. I was getting error
zipimport.ZipImportError: can't decompress data; zlib not available
What ultimately solved it for me was reading through this thread
https://github.com/jiansoung/issues-list/issues/13

Failing to create a .exe file with Python and PyQt5

I`m trying to make an executable from my python script called ProyectoNew.py. It works with a folder called "Imagenes" and another called "ModulosExternos", and a PyQT5's .ui file like this:
Here`s the Code posted in GitHub: https://github.com/TheFlosh/ProyectoSoftware.git
I`ve tried to use Pyinstaller, Py2Exe and CXFreeze but it didn't worked. Using each one of these modules to create a .exe file I've got the same result when I tried to execute it in another PC, as shown here:
On the LEFT of the picture you can see what I get after using pyinstaller (or Py2Exe), on the right you can see what I need to show.
Here are the modules that I use in my code ("ModulosExternos" is the folder where I put some particular modules that my code needs):
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import pyttsx3
from pyttsx3.drivers import sapi5
import PyQt5
import sip
import os
from ModulosExternos import Boton_1,Boton_2,Boton_3,Boton_4,Boton_Final,Listas_Pictogramas, Frases_Completas
And here is the last part of it, to instantiate the GUI:
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
GUI = ProyectoNew()
GUI.show()
sys.exit(app.exec())
I`ve read a lot of posts on the internet that recommended creating Setup.Py file to initiate the process of creating an executable file of my project. These are some examples of what I did with two of them:
With CX_Freeze:
import sys
import os
from cx_Freeze import setup, Executable
files = ['icono.ico','/Imagenes']
target = Executable(
script = "/ProyectoNew.py",
base = 'Win32GUI',
)
#Setup
setup(
name = "Proyect2",
version = "1.6",
description = "Software",
author = "----",
options = {'build_exe': {'include_files' : files}},
executables = [target]
)
With Py2Exe:
from cx_Freeze import setup, Executable
setup(name = "Proyect2",
version="1.0",
description = "Software",
executables = [Executable("ProyectoNew.py")],)
With Pyinstaller I typed: python --nowindowed --onefile ProyectoNew.py but I constantly got the same result as shown before.
I think that when I execute Pyinstaller, the .exe file doesn`t load the modules and the images that I use. What am I missing while creating the file? What do I need to do to execute the .exe file in another PC?.
I would prefer to use pyinstaller,but using any one of these will help me.
What's is wrong with that? You get some kind of error? Please edit your question with that.
With pyinstaller and this command, you should be able easily build an executable file:
pyinstaller [FILE].py -w -F
This command generate a /dist folder with the neccesary files and .exe file to run
*-w param is for do not provide a console window for standard i/o
*-F param is for one-file bundled executable
You can check more params here
PD: Before, you need obviously install pyinstaller:
pip install pyinstaller

.exe created with cxfreeze-quickstart does nothing - cx_freeze 5.0

I am using python 3.5.1 and the unofficial cx_freeze 5.0 build available from here. I am trying to create an executable version of a python project using tkinter and sympy that I've been working on. I used cxfreeze-quickstart to create a setup.py file for the program, and in terms of building what at least seems to be a valid executable, it works without throwing any errors. However, when I try to run the executable, nothing happens. I know similar questions have been asked on here and I've looked at and tried to understand every one I've found, but none of the solutions have worked for me. I don't understand what's going on, and any help would be appreciated. Code below:
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need
# fine tuning.
buildOptions = dict(packages = [], excludes = [])
import sys
base = 'Win32GUI' if sys.platform=='win32' else None
executables = [
Executable('c:\\users\\joe\\pycharmprojects\\physics2-0\\physics2-0.py', base=base,
targetName = 'c:\\users\\joe\\pycharmprojects\\physics2-0\\physics.exe')
]
setup(name='physics solver',
version = '0.1',
description = 'alpha physics solver',
options = dict(build_exe = buildOptions),
executables = executables)
Thanks in advance.
UPDATE: I am now attempting to write the setup.py script myself according to the template provided in the docs, although any help would still be greatly appreciated.
UPDATE 2: I wrote my own setup.py according to the template provided in the docs, and put it in the same folder as the script I want to freeze, something I hadn't realized I needed to do. I ran python setup.py build in command line, and it created the build subdirectory with the exe and DLLs. However, now when I try and run the exe, an error message pops up that says ImportError: DLL load failed. The specified module could not be found. in reference to tkinter. The code for the 2nd setup.py is below.
import sys
from cx_Freeze import setup, Executable
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["tkinter", "sympy", "_tkinter"], "excludes": []}
# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == 'win32':
base = "Win32GUI"
setup( name = "physics solver",
version = "0.1",
description = "a basic physics solver",
options = {"build_exe": build_exe_options},
executables = [Executable("Physics2-0.py", base=base)])
Below are the first 4 lines of physics2-0.py. The line brought into question by the error message is line 1.
from tkinter import *
from tkinter import ttk
from sympy import Symbol
from sympy.solvers import solve
UPDATE 3: Someone please help me out here. I can't figure this out. I've even done a clean re-install of python at this point, just to be sure I didn't accidentally mess something up at some point, and it's still giving me the same error message.

PYQT resources, frozen programs

Issue/question with PyQT / resources / cx_freeze on Windows:
I'm using QT's resource system to set an application icon. (Goes in the top left corner of the Window for Windows programs) I created the resource in Designer, then used pyrcc4 to create a rc.py file. It works properly on my uncompiled program, but fails to show (Shows the generic windows program icon instead) when compiling the script with cx_freeze. Note that I am not referring to the icon you click to launch the program - that's not handled by QT, and works properly. Any ideas? This is my setup.py.
from sys import platform
from cx_Freeze import setup, Executable
import module_locator
_dir = module_locator.module_path()
base = None
if platform == "win32":
base = "Win32GUI"
setup(
name = "Plates",
version = "0.1",
description = "Downloads approach plates",
executables = [Executable(_dir + '\\plates.pyw',
base = base, icon = _dir + '\\icon.ico')],
)
I get no errors when building the program. My rc file does exist (as a compiled python file) in library.zip.
Reposting as an answer:
Qt needs plugins to display some image formats. Look for a folder called 'imageformats' and copy it into your application directory (next to the exe).
The next version of cx_Freeze should find and copy imageformats automatically when you use QtGui.

How can I hide the console window when freezing wxPython applications with cxFreeze?

I'm developing a Python application using wxPython and freezing it using cxFreeze. All seems to be going fine apart from this following bit:
When I run the executable created by cxFreeze, a blank console window pops up. I don't want to show it. Is there any way I could hide it?
It doesn't seem to be documented on the cxFreeze site and Googling didn't turn up much apart from some similar sorta problems with Py2Exe.
Thanks.
For Windows:
You have to use a line like this (use file folders and names as appropriate)
C:/Python/Scripts/cxfreeze C:/Python/Code/yourprogram.py --base-name=Win32GUI --target-dir C:/Python/Dist
By adding the --base-name=Win32GUI option, the console window will not appear.
This worked to some extent but it has issues. My program runs in both a console mode and a GUI mode. When run from the console with a --console argument it runs in a console mode. When I followed the procedure below, this doesn't work anymore and my program is only a GUI app then.
The following source code comes from a sample file in the \Python\Lib\site-packages\cx_Freeze\samples\PyQt4\setup.py. Lesson of the day. Read the README.
# A simple setup script to create an executable using PyQt4. This also
# demonstrates the method for creating a Windows executable that does not have
# an associated console.
#
# PyQt4app.py is a very simple type of PyQt4 application
#
# Run the build process by running the command 'python setup.py build'
#
# If everything works well you should find a subdirectory in the build
# subdirectory that contains the files needed to run the application
import sys
from cx_Freeze import setup, Executable
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup(
name = "simple_PyQt4",
version = "0.1",
description = "Sample cx_Freeze PyQt4 script",
executables = [Executable("PyQt4app.py", base = base)])
If you're using Windows, you could rename your "main" script's extension (that launches the app) to .pyw
Option 1) Use gui2exe to muck with various options.
Option 2) Modify your setup.py with 'base' parameter as such.
GUI2Exe_Target_1 = Executable(
# what to build
script = "rf_spi.py",
initScript = None,
base = 'Win32GUI', # <-- add this
targetDir = r"dist",
targetName = "rf_spi.exe",
compress = True,
copyDependentFiles = False,
appendScriptToExe = False,
appendScriptToLibrary = False,
icon = r"wireless.ico"
)

Categories

Resources