I am trying to build a very basic executable (Windows) using PySide. The following script runs properly in the interpreter (Python 2.7, PySide 1.1.2)
#!/usr/bin/python
import sys
sys.stdout = open("my_stdout.log", "w")
sys.stderr = open("my_stderr.log", "w")
import PySide.QtGui
from PySide.QtGui import QApplication
from PySide.QtGui import QMessageBox
# Create the application object
app = QApplication(sys.argv)
# Create a simple dialog box
msgBox = QMessageBox()
msgBox.setText("Hello World - using PySide version " + PySide.__version__)
msgBox.exec_()
I tried 3 methods (py2exe, pyinstaller and cx_freeze) and all the 3 generated executables fail to execute. The two stdout/stderr files appear, so I found the first PySide import is making everything fail. (Unhandled exception/Access violation)
I analyzed the executable file with depends (http://www.dependencywalker.com/) and everything looks correctly linked.
Any idea?
You need to add the atexit module as an include. source: http://qt-project.org/wiki/Packaging_PySide_applications_on_Windows
(is also the case for Linux btw)
Thank you for your help. Actually, this did not change anything :/ However, I found a solution to my problem: if I add from PySide import QtCore, QtGui, then the executable (with pyinstaller) does work!
Related
Issue
I used Qt Design Studio to create some UI components and screens (.ui.qml format), but no matter what I do, whenever I try to run it in PyQt5 I kept encountering the following error:
module "QtQuick.Studio.Components" is not installed
The .ui.qml file has an import
import QtQuick.Studio.Components 1.0
I looked everywhere online and couldn't find much on the topic on how to fix this problem.
Question
What's the best way to overcome this issue? I've been using it on Windows but I am mainly looking to use the code on a raspberry pi, are there extra dependencies that need to be installed?
Python code
import os
from pathlib import Path
import sys
from PyQt5.QtCore import QCoreApplication, Qt, QUrl
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine
if __name__ == '__main__':
CURRENT_DIRECTORY = Path(__file__).resolve().parent
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.addImportPath(os.fspath(CURRENT_DIRECTORY.parents[0]))
url = QUrl.fromLocalFile(os.fspath(CURRENT_DIRECTORY / "Screen01.ui.qml"))
def handle_object_created(obj, obj_url):
if obj is None and url == obj_url:
print("exit")
QCoreApplication.exit(-1)
engine.objectCreated.connect(
handle_object_created, Qt.ConnectionType.QueuedConnection
)
engine.load(url)
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec())
Windows
I used print(engine.importPathList()) to see my import paths. This prompted me to go C:/Users/USER/AppData/Local/Programs/Python/Python38/lib/site-packages/PyQt5/Qt5/qml and noticed that QtQuick exists but does not have a Studio folder.
Since Qt Design Studio can run my code with no import issues, I knew that it would have a Studio folder. Therefore, I went into C:\Program Files\Qt\qtdesignstudio-2.2.0-community\qt5_design_studio_reduced_version\qml\QtQuick and copied the Studio folder from there onto the PyQt5 path above.
This fixed the issue.
Linux
I am yet to try it on Linux.
Running my GUI application created in PyQt5 using any IDLE or the file.py is running perfectly, however, when I converted the .py to .exe using Pyinstaller
I get an error every time I try to run the .exe file for some reason a small command window pops with an error and immediately disappear I screenshot the error before it closes. Any Idea Thanks in advance
Error image.
I tried to execute different commands for pyinstaller but no luck.
<pyinstaller filename -F >
<pyinstaller filename -onefile >
<pyinstaller filename >
It's an app over 900 lines and I cannot upload all of that but I think according to the error first lines.
The error occurs so here are the lines of code.
The problem is within importing modules I believe.
from PyQt5.QtWidgets import QWidget
from PyQt5 import QtCore, QtGui, QtWidgets
from datetime import datetime
import time
import os, sys #importing system modules
class Ui_MyTrophiesWindow(object):
class save_txt_file(QWidget):
def GetSaveFile(self):
path = QFileDialog.getSaveFileName(self,"Save MyTrophies.txt here", "MyTrophies", "*.txt")
working_path = os.path.abspath(__file__)[0:-13]
file = open(str(working_path) + "Txtpath.dat", "w+")
for i in path:
file.write(str(i))
file.close()
It is very common that sometimes PyInstaller won't recognize all Qt5 dependencies especially QT core DLLs. A simple way is to just locate that file on your current Python path and feed it with add-data flag in PyInstaller. Also, I suggest you not to use upx with PyQt as it may corrupt some DLLs:
pyinstaller --noupx -F --add-data "<Python_path>/Lib/site-packages/PyQt5/Qt/bin/Qt5Core.dll;./PyQt5/Qt/bin" script.py
To verify the answer suppose below example:
import traceback
import os
import sys
def func():
from PyQt5.QtWidgets import QWidget
from PyQt5 import QtCore, QtGui, QtWidgets
try:
if getattr(sys, 'frozen', False):
print(sys._MEIPASS)
func()
print("Import OK!")
except Exception:
traceback.print_exc()
input()
After you run the executable you would see the path for Pyinstaller (something like C:\Users\User\AppData\Local\Temp\_MEI90202), go to this path and search for the Qt5Core.dll and check if it is there.
I had look your error and according to me, I think you need to (re)install Qt5core.dll module and to add it in the site-package/PyQt5/init.py path. You can download the dll file here:
http://www.telecharger-dll.fr/dll-Qt5Core.dll.html
Good evening
Make sure that you have all the items of that app in the correct folder. Let me put an example that happened to me days ago:
I had an app with a folder named "options" that had 3 files (2 icons for my buttons ui and a .ini file). When I created the pyinstaller version of my program I assumed that it would somehow copy those files and pack them inside the --onefile file or into the dist folder (if not --onefile command was used). Nope, it didn't.
After scratching my head for hours, I just copied the options folder from my source files and pasted it next to my --onefile file (or inside the dist folder).
After that, my app works without issues. So... make sure it has all the files it needs in the correct folders.
Hi I tried to use a animation script
free meshcache for maya
# this is the error that I have ( No module named PyQt4 # )
Error: line 1: ImportError: file C:/Users/Lav/Documents/maya/2016/scripts/meshCache_script.py line 5: No module named PyQt4 `
#I am using maya 2016 with service pack in window 7 64 bit
Is it possible to change this script, so that everybody can use without dealing with pyqt4 install or others.
If I could use this script, I really appreciate for you.
Thank you guys.
In the specific case of that script it should be as easy as replacing:
from PyQt4 import QtGui, QtCore
import sip
with:
from PySide import QtGui, QtCore
import shiboken
and then, on line 32, replacing:
return sip.wrapinstance(long(controlPoniter), QtCore.QObject)
with:
return shiboken.wrapInstance(long(controlPoniter), QtGui.QWidget)
Then follow the instructions here (where I assume you got the script) and it should work.
What I suggested is what I believe the author was doing with the meshCache_script_2016.py file he mentions, in the page linked above. I downloaded the zipped file from there but couldn't find meshCache_script_2016.py in there, so you might want to ask the author if he omitted it on purpose (in case he realised that other parts of his script and/or plugin would break in Maya 2016, anyway).
I'm working on a desktop application using pyqt5, and I want to use a Virtual Keyboard with Handwriting Recognition. I saw that Qt, QtVirtualKeyboard already support it.
Here's a link!
I got the C++ Qt example code running on QtCreator.
But using python3.5 and PyQt5 it gives this message:
module "QtQuick.VirtualKeyboard" is not installed
import QtQuick.VirtualKeyboard 2.1
How should I go on from here?
Does PyQt5 comes with VirtualKeyboard module? if no How to install it on PyQt5?
for qt desinger you can add only this line on your .py file.
os.environ["QT_IM_MODULE"] = "qtvirtualkeyboard"
but If you want use QML with qtvirtualkeyboard;
There is no virtualkeyboard plugin in pyqt5.8, you must be use qt's paths.
For a example, basic steps for pyqt5, qt5.8 and qtvirtualkeyboard installiation on ubuntu:
1.step install qt5.8 with qtvirtualkeyboard
wget
http://download.qt.io/official_releases/qt/5.8/5.8.0/qt-opensource-linux-x64-5.8.0.run
chmod +x qt-opensource-linux-x64-5.8.0.run
./qt-opensource-linux-x64-5.8.0.run
2.step
apt-get install python3 python3-pip
pip3 install pyqt5
3.step
set environmental variables your qt paths on your python code.
import sys, os
os.environ["QT_DIR"] = "/opt/Qt5.8.0/5.8/gcc_64"
os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = "/opt/Qt5.8.0/5.8/gcc_64/plugins/platforms"
os.environ["QT_PLUGIN_PATH"] = "/opt/Qt5.8.0/5.8/gcc_64/plugins"
os.environ["QML_IMPORT_PATH"] = "/opt/Qt5.8.0/5.8/gcc_64/qml"
os.environ["QML2_IMPORT_PATH"] = "/opt/Qt5.8.0/5.8/gcc_64/qml"
os.environ["QT_IM_MODULE"] = "qtvirtualkeyboard"
#print(os.environ)
from PyQt5.QtCore import *
from PyQt5 import QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtQuick import *
class keyboardapp(object):
def __init__(self):
self.view = QQuickView()
self.view.setObjectName("View")
#self.view.setFlags(Qt.FramelessWindowHint)
self.view.setSource(QUrl("main.qml"))
self.view.setResizeMode(QQuickView.SizeRootObjectToView)
#self.Screen = self.view.rootObject()
#print("Screen(Root) = " + str(self.Screen))
self.view.show()
app = QApplication(sys.argv)
test = keyboardapp()
sys.exit(app.exec_())
I been stuck with this too, and i am new to Qt
After some researching, and digging in source code, found the solution
You dont need to import it to use it since its a module it will implement itself to every qt input
Well you dont need to install it on PyQt5 but on Qt5 if it is not already come with your Qt package, if you use archlinux you can install it with pacman
pacman -S qt5-virtualkeyboard
If you cannot find it in you os repositories try to build it here is documantation
https://doc.qt.io/qt-5/qtvirtualkeyboard-index.html
Then to use it in your pyqt application, set environmental variable QT_IM_MODULE to "qtvirtualkeyboard" either from your bash or inside the top your script like
import os
os.environ["QT_IM_MODULE"] = "qtvirtualkeyboard"
I have a project with a GUI created in Qt Designer and compiled with pyuic. So I import QtCore and QtGui from PyQt4.
The script runs OK.
I need an executable file. The tool is PyInstaller, target platforms - Linux and Windows. I've tried and had a success once. Then I developed a project for some time and now... I cannot make an executable - it crashes with
ImportError: No module named QtCore
The problem is that I cannot compare current project with the old one. And I'm not sure how the environment in my PC has changed.
So I have to understand why PyInstaller makes an executable with no error messages - but the program crashes. Or how to help PyInstaller (I've read manual and tried a lot from it but to no avail).
Here is a simplified version of my project (actually a single file) that has the main feature: it runs OK from Python and crashes as standalone program.
#!/usr/bin/python
# -*- coding: utf-8 -*-
""" test script to learn PyInstaller usage
"""
import sys
from PyQt4 import QtCore
from PyQt4 import QtGui
class Main(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui = Ui_ADCmonitor()
self.ui.setupUi(self)
self.exitAction = QtGui.QAction('Exit', self)
self.exitAction.setShortcut('Ctrl+Q')
self.exitAction.triggered.connect(self.close)
toolbar = self.addToolBar('Exit')
toolbar.addAction(self.exitAction)
self.refresh = QtCore.QTimer()
self.status_freeze_timer = QtCore.QTimer()
self.update_data()
def update_data(self):
self.status_refresh('Ok')
self.refresh.singleShot(200, self.update_data)
def status_refresh(self, msg):
self.ui.statusbar.showMessage(msg)
class Ui_ADCmonitor(object):
def setupUi(self, ADCmonitor):
ADCmonitor.setObjectName("ADCmonitor")
ADCmonitor.resize(300, 300)
self.statusbar = QtGui.QStatusBar(ADCmonitor)
self.statusbar.setObjectName("statusbar")
ADCmonitor.setStatusBar(self.statusbar)
QtCore.QMetaObject.connectSlotsByName(ADCmonitor)
ADCmonitor.setWindowTitle("ADC Monitor")
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = Main()
window.show()
sys.exit(app.exec_())
And please don't propose me to use other dist utilities. I tried some of them, I've chosen PyInstaller and I'd like to use it.
So, the problem is in a PyInstaller 2.1 bug - as Joran Beasley supposed.
Solution:
sudo pip install git+https://github.com/pyinstaller/pyinstaller.git
And bingo!
pyinstaller myscript.py makes correct exec.