Problem: The following program is keep starting itself every 5 seconds after making it executable using PyInstaller. It keeps running itself again and again. The code works fine without making if I run it directly using python.
I spent more than a day to fix this issue but no luck so far.
I tried it on Windows and Mac. Python version 3.7, PyInstaller Version 3.5 & 3.6.
PyInstaller Command:
pyinstaller --onefile generic_code_edit.py
Python Code:
import logging
logging.basicConfig(level=logging.DEBUG)
import sys
from pyqode.qt import QtWidgets
from pyqode.core.widgets import GenericCodeEdit
def main():
app = QtWidgets.QApplication(sys.argv)
# create editor and window
window = QtWidgets.QMainWindow()
editor = GenericCodeEdit()
# open a file
editor.file.open(__file__)
window.setCentralWidget(editor)
# run
window.show()
app.exec_()
editor.file.close()
if __name__ == "__main__":
main()
The Debug log:
DEBUG:pyqode.core.cache:getting encoding for generic_code_edit.py
DEBUG:pyqode.core.managers.file:mimetype detected: text/x-python
DEBUG:pyqode.core.managers.file:file open: generic_code_edit.py
ERROR:pyqode.backend:DEBUG:pyqode.qt:importing PyQt5
ERROR:pyqode.backend:DEBUG:pyqode.qt:imported PyQt5
ERROR:pyqode.backend:INFO:pyqode.qt:using pyqt5
ERROR:pyqode.backend:DEBUG:pyqode.core.cache:getting encoding for
generic_code_edit.py
ERROR:pyqode.backend:DEBUG:pyqode.core.managers.file:mimetype
detected: text/x-python
ERROR:pyqode.backend:DEBUG:pyqode.core.managers.file:file open:
generic_code_edit.py
Update:
Finally I found the problem. It is related to the autocomplete feature. This feature does not load perfectly with pyinstaller and cause a new start. Although I could not be able to make it working with pyinstaller, but I have been able to stop these feature to solve the starting loop issue.
I have passed an empty server file as backend to solve the issue. But still I want to make the Autocomplete working with pyinstaller.
editor = GenericCodeEdit(None, 'empty_file.py')
Related
Simply stated I have a simple python application which generates random passwords. This application was originally written using Tkinter and currently works. I am trying to improve the GUI interface by employing PyQt5. My efforts, so far have resulted in an application that runs from within my IDE (Spyder) and can also be run by invoking python from the commandline with the fullpath of the python script.
It should be noted this works for the Tkinter as well as the PyQt implementation.
My next step was to define a shortcut on the desktop to execute this script and have a window appear allowing creation of a password. The shortcut for the Tkinter script performs as expected and results in a window appearing. The script for the PyQt5 based script does not work. The only differences between the scripts are the target files being invoked by the script. Also both script files are in the same directory. This is a side by side image of the shortcut properties.
Here is a very simplistic example of the Puqt5 code. This code exhibits the same characteristics as the original in that it runs in the IDE as well as directly from Python in the CMD window but will not execute from a shortcut icon.
"""
Created on Wed Sep 9 10:37:46 2020
"""
import sys
from PyQt5.QtWidgets import QApplication, QWidget
def main():
app = QApplication(sys.argv)
w = QWidget()
w.resize(250, 150)
w.move(300, 300)
w.setWindowTitle('Simple')
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Has anyone else had this type of problem or can anyone suggest an approach for determining what is the cause and solution
everything works on Windows 7
It work when invoked from a shortcut on Win &, hmm that's very strange.
I tested it on Windows 10 also works.
Thanks for the helpful suggestions as well as your efforts to test this problem on your own systems. I finally found an answer that satisfies my needs. I used pyinstaller to create an executable and then launch the executable from a desktop shortcut. This works, so I am considering my question closed.
I can't manege to get the images or icons loaded. When I run the app in VScode the app works, just without the images. The Icons even have the space but there is nothing in the space where the icon is supposed to be. But running it in the Command Terminal works like it is supposed to.
Using the 'os.path. ...' method, and running it in VScode works every time. But why is it like that?
And, how will the os.path method affect the app when I run it alone? I.e. when I don't run it though the IDE or the Command Prompt (I'm planing to make it a stand alone app, just for fun).
A sample of code that works, the whole app is a bit bigger. (but same principles):
import sys
import os
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My App")
current_directory = os.path.dirname(os.path.realpath(__file__))
widget = QLabel("")
image = QPixmap(os.path.join(current_directory, "39487.jpg" ))
widget.setPixmap(image)
widget.setScaledContents(True)
self.setCentralWidget(widget)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
Now why when i run this code, the images show. Yet when I remove the os.path method it does not work in the IDE. Running the second version in the Prompt does work though.
Why does the IDE load the code different than the Prompt?
The relative paths in python with respect to where the python command is executed, by default VScode launches python in the root folder of the project so it is not necessarily launched in the script folder, instead you use the command promt from the folder where There is the script and the icons, if you were to run from a higher folder you would also have the same problem. Due to the error that causes relative paths it is better to use absolute paths either explicitly or by constructing it using os.path.
I'm trying to run an app with PySide2 from Spyder 3.2.8 and Python 3.6.4 in Anaconda in a macOS 10.13.4.
attempt N°1
After having seen this stackoveflow page and this github page I changed my graphic backend from Inline to Automatic in Python > Preferences > IPython Console > Graphics and I tried to run the following script (script N°1):
script N°1
import sys
from PySide2.QtWidgets import *
# Create a Qt application
app = QApplication.instance()
if app is None:
print("print something")
app = QApplication(sys.argv)
# Create a Label and show it
label = QLabel("Hello World")
label.show()
# Enter Qt application main loop
app.exec_()
but got the following error message after running it:
Importing PySide2 disabled by IPython, which has
already imported an Incompatible QT Binding: pyqt5
There are similar reported issues here with matplotlib and here with ipython but it didn't help me (or I couldn't implement it properly). Then I tried to implement the content of this page about qtpy by changing the script N°1 in the following way:
script N°2
import os
os.environ['QT_API'] = 'pyside2'
from qtpy.QtWidgets import *
import sys
# Create a Qt application
app = QApplication.instance()
if app is None:
print("print something")
app = QApplication(sys.argv)
# Create a Label and show it
label = QLabel("Hello World")
label.show()
# Enter Qt application main loop
app.exec_()
attempt N°2
With Inline selected in Python > Preferences > IPython Console > Graphics. When I ran the script N°2 , the app launches and I got print something printed in to the console. When closing the app, I got Out[1]: 0 in the console. However when I run the script again, no error message appears in the console but the window of the app doesn't show-up
attempt N°3
This time with Automatic selected in Python > Preferences > IPython Console > Graphics. When I ran the script N°2 the first time, the app didn't launch and I got the following error message
/anaconda3/lib/python3.6/site-packages/qtpy/__init__.py:178: RuntimeWarning: Selected binding "pyside2" could not be found, using "pyqt5"
'using "{}"'.format(initial_api, API), RuntimeWarning)
Out[2]: -1
attempt N°4
With Automatic selected in Python > Preferences > IPython Console > Graphics. When I ran the script N°1 after having changed the line from PySide2.QtWidgets import * to from PyQt5.QtWidgets import *: The app didn't launch and I got the following error message
Out[1]: -1
attempt N°5
With Inline selected in Python > Preferences > IPython Console > Graphics. When I ran the script N°1 after having changed the line from PySide2.QtWidgets import * to from PyQt5.QtWidgets import *: The app launches and I got print something printed in to the console. I closed the app and got Out[1]: 0 in the console. However when I run the script again, no error message appears in the console but the window of the app doesn't show-up
N.B. this question is the continuation of that question
(Spyder maintainer here) Since the ipykernel package (which is used by Spyder to run code in its consoles) doesn't have event loop support for PySide2 as of May/2018 (as can be seen here), you won't be able to run PySide2 code inside Spyder, no matter what you try.
Notes:
The Automatic backend tries to select a suitable event loop for you, in this order: Qt5, Qt4, Tk and Inline. That's why it doesn't work in your case.
Every time you change a Graphics backend in Spyder, you need to restart the kernel of the console you want to run your code in. That's because you can only use one backend per console session (this is a limitation imposed by ipykernel, not by us). It's clear from your question that you're not doing that.
We're aware we fail to inform users when a kernel restart is necessary. We'll try to address that in our next major version (Spyder 4), to be released in 2019.
If you already know about qtpy, please use it to develop your apps instead of using PySide2 directly. That way you could work with PyQt5 for development in Spyder, but PySide2 for deployment, since qtpy takes care of working seamlessly with whatever binding is available.
I have created a large python program with GUI based on PyQt4. I would like the package to run both in an IPython notebook (old installation with Python 2.7 on windows), Jupyter notebook (Python 3.5 installed recently with Anaconda), and as a python program passed on the command line. I'm having problems in running the code in Jupyter notebook (see directly at the bottom).
My module mymodule.py looks like this (extremely simplified, about 10k lines before the show from many other python files):
from PyQt4 import QtCore, QtGui
class MyModule(object):
def __init__(self):
self.window = QtGui.QMainWindow()
self.window.show()
The typical usage from command line is
python myscript.py
with the following file myscript.py
from PyQt4 import QtCore, QtGui
import mymodule
m = mymodule.MyModule()
APP = QtGui.QApplication.instance()
APP.exec_()
This works fine. I understand that APP.exec_() is needed to start some kind of EventLoop that works through the Gui interaction events.
In an IPython notebook, the user typically does
import mymodule
m = mymodule.MyModule() # opens the gui
# this still leaves the console active to allow things like this:
m.change_color("red")
I can run this without problems, where I understant that IPython somehow takes care of the EventLoop behind the scene.
Now, running the same commands in Jupyter notebook, a window opens, but freezes before allowing any user interaction. So I believe that Jupyter notebook does not treat the events properly because I did not tell it to do so. One way I have found is executing the command %pylab before running my code. However, I frequently run into related problems with this, for example when running %pylab and %matplotlib inline in direct succession before starting my program, this leads to freezing again once I load my code (curiously, reversing the order of the two magical commands works again). Also, I do not want to force the user of my program to execute %pylab in each new notebook if it can be avoided (also because I believe this requires a matlab installation, which is not a requirement of my program).
What code must I add in mymodule.py to make things compatible with the described user code in Jupyter notebook? Can anyone explain more clearly how IPython notebook and Jupyter notebook manage the QEventLoop/QApplication (or whatever is the important concept here) differently, and how the magic commands mess with this? I am afraid of hidden bugs in my program because of this, and would like to make it as robust as possible to not frustrate the users.
Here is a working solution that allows to run the code without distinguishing between different IPython / Jupyter versions and 'raw' Python. My __init__.py file contains this section at the beginning:
# enable IPython QtGui support if needed
try:
from IPython import get_ipython
get_ipython().magic('gui qt')
except BaseException as e:
# issued if code runs in bare Python
print('Could not enable IPython gui support: %s.' % e)
# get QApplication instance
from PyQt4 import QtCore, QtGui
APP = QtGui.QApplication.instance()
if APP is None:
print('Creating new QApplication instance "mymodule"')
APP = QtGui.QApplication(['mymodule'])
The script running on raw Python then only needs this:
import mymodule # imports the above code
from PyQt4 import QtCore, QtGui
if __name__ == '__main__':
QtGui.QApplication.instance().exec_()
I found no use case where this does not work.
I am trying to create an OS X application from this code:
import sys
from PyQt5.QtWidgets import QApplication
if __name__ == "__main__":
app = QApplication(sys.argv)
app.lastWindowClosed.connect(app.quit)
sys.exit(app.exec_())
I'm in OS X 10.10 using py2app (0.9.1) and PyQt5 (5.4.0) in Python 2.7.9. The app seems to be built just fine. Upon launching the app, it immediately crashes with "Abort trap: 6".
I've tried using the default setup.py created by py2applet as well as generating the app directly from py2applet. The python file itself runs without crashing.
Any ideas/help would be greatly appreciated!
You import QApplication and then you try to instantiate it using the package name: QtWidgets.QApplication Apart from that, I used the following setup.py and it worked:
from setuptools import setup
setup(
app=["MyApplication.py"],
setup_requires=["py2app"],
)
Running the command python3 setup.py py2app -A --packages=PyQt5 produced a working app. When you get an error, check Console for any output. This can help when debugging.
As a side note, when I pack PyQt app, I usually also copy over any Qt pluggins and run the macdeployqt tool to fix linking.:
cp -R /Developer/Qt/5.3/clang_64/plugins/ dist/MyApplication.app/Contents/PlugIns
macdeployqt dist/MyApplication.app
After struggling with this problem for a while and not being able to find a solution, I switched to PyInstaller which worked for me with PyQt 5.6.1 out of the box. I detailed some of my findings and why I ended up picking PyInstaller over other available freezers in a blog post.