I have an issue with PyQt5 and VS Code. I have installed PyQt5 on my computer but when I write from VS Code it doesn't work. VS Code underlines my imports with a yellow line and when I run the code it says
from PyQt5 import QtWidgets
ImportError: No module named PyQt5.
However, when I run the same program from my terminal, it runs fine. My code is
import sys
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QDialog, QApplication, QWidget,QStackedWidget
class Window(QDialog):
def __init__(self):
super(Window, self).__init__()
loadUi("window.ui",self)
app= QApplication(sys.argv)
window = Window()
widget = QStackedWidget()
widget.addWidget(window)
widget.setFixedHeight(800)
widget.setFixedWidth(1200)
widget.show()
try:
sys.exit(app.exec())
except:
print("Exiting")
There should be more than one python version installed on your computer, please select the correct python interpreter using:
Ctrl+Shift+P command to open the Command Palette
search for and select Python:Select Interpreter
(Or click directly on the python version displayed in the lower right corner)
select the correct interpreter.
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.
i have few web based games on html and java-script , want to open it inside pyqt5 window in python.
i was trying it on tkinter but failed to render html in it.
need help
Thanks.
Suppose this game (contra) want to play it inside pyqt5 window
screenshot of pycharm package installer
Screenshots of IDLE
Screenshot pycharm
This isn't using PyQt5 but you could launch a temp web server using SimpleHTTPServer
$ python2 -m SimpleHTTPServer 8000
or
$ python3 -m http.server 8000
It will serve up whatever is in the folder you run the command in. This shouldn't be used for production as it only provides the most basic security checks.
https://www.poftut.com/how-to-run-and-use-simple-http-server-in-python2-and-python3/
A simple solution is to use QWebEngineView that comes in the PyQtWebEngine package
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
view = QtWebEngineWidgets.QWebEngineView()
url = "https://www.retrogames.cz/play_022-NES.php?language=EN"
view.load(QtCore.QUrl(url))
view.show()
sys.exit(app.exec_())
You could load the page in a QWebView:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import QWebEngineView as QWebView, QWebEnginePage as QWebPage
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl("https://oldgameshelf.com/contra-208.html"))
web.show()
sys.exit(app.exec_())
If you need to scroll to a particular element, you could pass JavaScript to web.runJavaScript() that does the scrolling.
In the example above, I used the oldgameshelf.com link of the game you linked to. But you could replace this with the URL of any page.
Also, make sure you install PyQtWebEngine if you haven't already:
pip install PyQtWebEngine
Since it's now in a separate package.
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 * "
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.
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!