QT creator (can't find '__main__' module in '') error - python

I have made a brand new project in "Qt Creator" but every time I run it (without even editing anything) i get this error:
20:28:30: Starting C:\Users\aonob\AppData\Local\Programs\Python\Python310\python.exe -u ""...
C:\Users\aonob\AppData\Local\Programs\Python\Python310\python.exe: can't find 'main' module in ''
20:28:31: C:\Users\aonob\AppData\Local\Programs\Python\Python310\python.exe exited with code 1
How can I fix this?
im using python 3.10.6
here is the main.py file code:
`
# This Python file uses the following encoding: utf-8
import sys
from pathlib import Path
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine
if __name__ == "__main__":
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
qml_file = Path(__file__).resolve().parent / "main.qml"
engine.load(qml_file)
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec())
`

I faced the same problem, but solved it by following the steps below.
temporarily remove all paths except main.py from .pyproject
run the project and it should work fine
restore the paths in .pyproject
should run without problems
Perhaps this bug is due to QtCreator's auto-detection thing for the main script not working properly, but I can get it to recognize the main script by following the above steps.

Related

Specify a locale when building an Android app with Python - Kivy

I build an android app with Python and Kivy. The app works fine but I can't print some localisation details in my own langage (french).
Here is a simple example I wanted to work, and I can't achieve this :
It compiles with
buildozer -v android debug
I can install it on my phone, but when it runs it failed, my program can't set up the locale (I got the error msg with logcat)
What is the correct way to localise properly an Android app with kivy ?
import os
import locale
from datetime import date
from kivy.app import App
from kivy.uix.label import Label
if os.name == "nt":
local = "fra"
else:
local = 'fr_FR.UTF-8'
locale.setlocale(locale.LC_ALL,local)
class CalendarApp(App):
def build(self):
# I'm french, I want this to return "janvier" in my app, not "january"
return Label(text=date(2022, 1, 1).strftime('%B'))
if __name__ == "__main__":
app = CalendarApp()
app.run()

Opening Windows Explorer to Specific Path in PyQt properly

I'm trying to open a specific folder from my PyQt program. I know I could've used webbrowser module
like this
import webbrowser, os
path="C:/Users"
webbrowser.open(os.path.realpath(path))
or I could've used os.startfile module like this
import os
path = "C:/Users"
path = os.path.realpath(path)
os.startfile(path)
or subprocess which isn't recommended on Qt platform. so I wonder how could you do it on PyQt properly (maybe using QProcess?). I don't want to open file or folder dialog because I just want to open the folder without doing anything with it. also, I want to save time for a future update on a different OS than Windows, so I didn't have to change this part. is it possible?. many thanks
A Qt cross-platform solution is to use QDesktopServices::openUrl():
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
app = QtWidgets.QApplication(sys.argv)
path = "C:/Users"
fullpath = os.path.realpath(path)
if not QtGui.QDesktopServices.openUrl(QtCore.QUrl.fromLocalFile(fullpath)):
print("failed")
I managed to use QProcess to open explorer on specific path without additional module (e.g webbrowser). I only need platform module to determine which platform the program is running, like this
self.path = os.path.abspath(os.path.dirname(sys.argv[0]))
self.pathOutput = os.path.join(self.path, "output")
def open_explorer(self):
self._process = QtCore.QProcess(self)
if platform.system() == "Windows":
self._process.start("explorer",[os.path.realpath(self.pathOutput)])
elif platform.system() == "Darwin":
self._process.start("open",[os.path.realpath(self.pathOutput)])

Using anything from QtQuick.Controls throws a protected module error

I am getting these errors, when i try to launch the program:
plugin cannot be loaded for module, cannot install type into protected module
Platform:
Python 3.8
PyQt5 5.15.0
Visual Studio Community 2019
Windows 10 Pro 1909
main python file (the whole thing is pretty much the example from here: https://codeloop.org/pyqt5-creating-first-window/ )
import numpy
import os
import sys
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtWidgets import QApplication
def main():
app =QApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load(os.path.join(os.path.dirname(__file__), "MainApp.qml"))
if not engine.rootObjects():
return -1
return app.exec_()
if __name__ == '__main__':
main();
The corresponding QML File "MainApp.qml":
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick 2.15
Window {
visible:true
width:600
height:400
color:"yellow"
title: "PyQt5 QML Window"
Button {
text: "Something"
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
}
}
Without using anything from QtQuick.Controls, it works, but as soon as i add the button, it generates these errors:
QQmlApplicationEngine failed to load component
file:///C:/Users/elano/Source/Repos/Projekt-1-SS2020/Projekt-1-SS2020/MainApp.qml:15:5: Type Button unavailable
file:///C:/Users/elano/vpqt/lib/site-packages/PyQt5/Qt/qml/QtQuick/Controls.2/qmldir: plugin cannot be loaded for module "QtQuick.Controls": Cannot install type 'VerticalHeaderView' into protected module 'QtQuick.Controls' version '2'
<Unknown File>: Cannot install type 'HorizontalHeaderView' into protected module 'QtQuick.Controls' version '2'
<Unknown File>: Cannot install element 'SplitHandle' into protected module 'QtQuick.Controls' version '2'
...
The file it tries to find (qmldir) does exist, and contains this:
module QtQuick.Controls
plugin qtquickcontrols2plugin
classname QtQuickControls2Plugin
depends QtQuick.Templates 2.5
designersupported
Does anyone know what is going on here? Is more information needed?
Okay, now i feel stupid. I already kinda had my answer in my code, but since it didn't get Pyside2 to work, i commented it out...
The problem was, that an environment variable was not set properly. Adding this after my import statements in my main file fixed it:
dirname = os.path.dirname(PyQt5.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QML2_IMPORT_PATH'] = os.path.join(dirname, 'qml')
No tutorial i found has ever mentioned this. Great.

How to use Pygubu - Python

I am trying to use pygubu from the first time to make better GUI's. I have installed it using pip and it has installed correctly. However when I try and run the example code Here (At the bottom of the web page) I get the error
AttributeError: module 'pygubu' has no attribute 'Builder'
I don't know if this code is correct or not. I have looked for ways to use this tool but all I can find are links and videos for installing it. I have also tried This video but can't figure out how to open/run/use. I am using python-idle if that is the issue. The code (if you don't want to follow the link) is:
# helloworld.py
import tkinter as tk
import pygubu
class HelloWorldApp:
def __init__(self):
#1: Create a builder
self.builder = builder = pygubu.Builder()
#2: Load an ui file
builder.add_from_file('helloworld.ui')
#3: Create the mainwindow
self.mainwindow = builder.get_object('mainwindow')
def run(self):
self.mainwindow.mainloop()
if __name__ == '__main__':
app = HelloWorldApp()
app.run()
I would appreciate any help. Also when I try installing it again - just to check - I get:
Pygubu is an application. Find it in files from C: drive. The code:
# helloworld.py
import tkinter as tk
import pygubu
class HelloWorldApp:
def __init__(self):
#1: Create a builder
self.builder = builder = pygubu.Builder()
#2: Load an ui file
builder.add_from_file('helloworld.ui')
#3: Create the mainwindow
self.mainwindow = builder.get_object('mainwindow')
def run(self):
self.mainwindow.mainloop()
if __name__ == '__main__':
app = HelloWorldApp()
app.run()
Is run after you have created your UI (The format that this application uses) and that the name of the UI specified is helloworld.ui.
Note that instead of helloworld.ui in the following line:
builder.add_from_file('helloworld.ui') You should insert the filename
(or path) of your just saved UI definition.
Note also that instead of 'mainwindow' in the following line:
self.mainwindow = builder.get_object('mainwindow') You should have the
name of your main widget (the parent of all widgets), otherwise you
will get an error similar to the following:
Exception: Widget not defined.
This link explains the usage better than one stated in question

WindowsContext: OleInitialize() failed: "COM error 0x80010106 RPC_E_CHANGED_MODE (Unknown error 0x0ffffffff80010106)"

I'm programming in python 3.4.4 (32 bits), in windows 8. I'm running an app with PyQt5, the app was working well, but since i installed pywinauto 0.6.4 to set the focus on other app with:
other_app = pywinauto.Application().connect(process=int(code))
other_app.top_window().set_focus()
if i run My_app = QApplication(sys.argv), in console appears the next warning message, with no other information:
QWindowsContext: OleInitialize() failed: "COM error 0x80010106 RPC_E_CHANGED_MODE (Unknown error 0x0ffffffff80010106)"
SetProcessDpiAwareness failed: "COM error 0x80070005 (Unknown error 0x0ffffffff80070005)"
I suspect that's because there is a conflict between the pywinauto and the PyQt5.QtWidgets.QApplication modules. After the "warning" the program runs good, but i still don't now how to fix it.
This is the Minimal, Complete, and Verifiable example:
from PyQt5.QtWidgets import QApplication
import pywinauto
import sys
def main():
app = QApplication(sys.argv)
app.exec_()
if __name__ == '__main__':
main()
According to this post a workaround is to use sys.coinit_flags = 2 and warning module.
import sys
import warnings
warnings.simplefilter("ignore", UserWarning)
sys.coinit_flags = 2
import pywinauto
from PyQt5.QtWidgets import QApplication, QMainWindow
def main():
app = QApplication(sys.argv)
w = QMainWindow()
w.show()
app.exec_()
if __name__ == '__main__':
main()
I was facing the same issue working with PySide2. In my case, the import order made the difference. Namely, I was using
import clr
which must be placed after
app = QApplication(sys.argv)
Try to import in this way:
from PyQt5 import QtWidgets
...
app = QtWidgets.QApplication(sys.argv)
w = QMainWindow()
w.show()
app.exec_()
This works for me.

Categories