python cross platform apps - python

I'm trying to make an app in CPython that should work on both linux and windows.
I'm using the webkit library, witch works fine on linux (Ubuntu 12.04), but I can't get it to work on Windows.
I know that I can compile my app into a Windows executable (.exe) with py2exe, but to do that it must work on my Windows machine.
The question is: Is there any way I can package my app under linux, so it will have it's dependencies (webkit) bundled with it, so it will work under Windows? Or is there any way to make a windows executable that doesn't need any dependencies from a python file, under linux?
Thank you!
EDIT:
Here is my code for the test app:
import gtk
import webkit
class Base:
def __init__(self):
self.builder = gtk.Builder()
self.builder.add_from_file("youtubeWindow.ui")
self.main_window = self.builder.get_object("main_window")
self.scrl_window = self.builder.get_object("scrl_window")
self.webview = webkit.WebView()
self.scrl_window.add(self.webview)
self.webview.show()
self.webview.open("http://youtu.be/o-akcEzQ6Y8")
self.main_window.show()
def main(self):
gtk.main()
print __name__
if __name__ == "__main__":
base = Base()
base.main()

Ok, so i couldn't get webkit to work on windows with GTK, but i found out that Qt provides an integrated WebKit module, so I donwloaded PySide (the Qt wrapper for python) and I tested it with this script:
import sys
from PySide import QtCore
from PySide import QtGui
from PySide import QtWebKit
class MainWindow (QtGui.QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.setGeometry(300,300,800,600)
self.setWindowTitle('QtPlayer')
web = QtWebKit.QWebView(self)
web.settings().setAttribute(QtWebKit.QWebSettings.PluginsEnabled, True)
web.load(QtCore.QUrl("http://youtu.be/Dys1_TuUmI4"))
web.show()
self.show()
def main():
app = QtGui.QApplication(sys.argv)
win = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Also i used GUI2EXE and *cx_Freeze* to package it into an .exe windows app. (Don't forget to include the modules atexit,PySide.QtNetwork, more details here)
A cool guide on Qt-Webkit can be found here (It usese PyQt, but it's compatible with Pyside)
Also a Pyside tutorial here

In order run your script on Windows you need to install Webkit and its bindings for Windows (libraries). The 2 links below provide the setup files and instructions.
http://opensourcepack.blogspot.com/2009/12/pywebkitgtk-windows-binary.html
http://opensourcepack.blogspot.com/2011/01/conservative-all-in-one-pygtk-installer.html
The second link provides a binary that installs all the needed libraries (a all-in-one package).

Related

PyQt5 Missing QtQuick.Studio Module

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.

PyQt4 script frozen by PyInstaller gives Fatal Error:"Failed to execute script xyz"

I am writing the following Python 3.5 script:
import sys
from PyQt4 import QtGui
class Window(QtGui.QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(50, 50, 500, 300)
self.setWindowTitle("HelloGUI")
self.show()
def run():
app = QtGui.QApplication(sys.argv)
win = Window()
sys.exit(app.exec_())
run()
I create executable using PyInstaller. It runs normally. Though when I try to run the executable on a different PC (which has no Python installed) than mine, it gives the following Fatal Error Message: "Failed to execute script [script-name]".
If someone has an idea how I can make my GUI programms portable, please leave a comment. Otherwise, if what I have in my mind cannot be done, please let me know.
Windows10 (64 bit), Python 3.5(32 bit), PyInstaller(3.2), PyQt4
I solved my issue using --noupx when I use pyinstaller. [PyQt5-Python 3.4]
Example;
pyinstaller.exe --onefile --windowed --noupx --icon=app.ico myapp.py
Check this if the codes above doesn't solve your problem.
Using python3.7-64bit and PyInstaller 3.6 does the job without any errors.

How to use PyQt5 in Visual Studio 2015?

Is there possible to use PyQt5 in Visual Studio 2015?
I installed PyQt and created python project. I get an error.
No module named 'PyQt5'
If there is a handler for this exception, the
program may be safely continued.
What do I have to do?
import sys
from PyQt5 import QtWidgets
def main():
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
button = QtWidgets.QPushButton("Hello world")
window.setCentralWidget(button)
window.show()
app.exec_()
if __name__ == '__main__':
main()
Try this:
In "Solution Explorer" choose "References"->"Add Reference"->"Browse"- tab, where select *.pyd files in (Python installation folder)\Python34\Lib\site-packages\PyQt4.
Then in code one can use something like this: "from Qt import * "

Executable from Python2 script w. PyInstaller: Qt4 module import error

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.

PySide "hello world": py2exe/pyinstaller/cx_freeze and access violation

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!

Categories