PySide Qt script does not start from Spyder, but works from the shell

I have a strange error in my project that uses PySide for its Qt GUI, and in response I try to test a simpler code that sets up the environment.

Here is the code that I am testing: https://stackoverflow.com/a/165343/

When I run this from my shell ( python test.py ), it works fine. However, when I run this script in Spyder, I get the following error:

 Traceback (most recent call last): File "/home/test/Desktop/test/test.py", line 31, in <module> app = QtGui.QApplication(sys.argv) RuntimeError: A QApplication instance already exists. 

If this helps, I also get the following warning:

 /usr/lib/pymodules/python2.6/matplotlib/__init__.py:835: UserWarning: This call to matplotlib.use() has no effect because the the backend has already been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot, or matplotlib.backends is imported for the first time. 

Why does this code work when launched from my shell, but not from Spyder?


Update: Mata replied that the problem is due to Spyder using Qt, which makes sense. At the moment, I installed execution in Spyder using the option "Run in an external system terminal", which does not cause errors, but also does not allow debugging. Does Spyder have any built-in workarounds?

0
source share
3 answers

Since Spyder also a Qt application, it starts its own QApplication . Only one QApplication can exist in the same process, so you get the first error.

Sypder also uses matplotlib , and it probably imports some of the mentioned modules already, so you will get a second error.

Therefore, when this is the case, you cannot create your own QApplication or call matplotlib.use() . Or maybe it will work if you complete these calls in try / except .

+2
source

I have the same problem and somewhere on stackoverflow there was a solution.

Instead

 qApp = QtGui.QApplication(sys.argv) 

Using

 qApp = QtGui.QApplication.instance() if qApp is None: qApp = QtGui.QApplication(sys.argv) 
+2
source

It will not work in Spyder if you try to run the application in an interactive console, because this console is specially configured to import several scientific libraries, automatically show() digits matplotlib and several other details. Type scientific at the Spyder command line for more details. As a result, the Qt application event loop works efficiently.

To run the application in Spyder:

  • Make sure Spyder is configured to open external consoles using PySide, not PyQt. This can be set in the menu Services> Settings> Console> Plugins> Select Qt-Python Binding Library.
  • Using the script active in the editor, press F6 to open the Launch Options dialog box. Select Run in New Specialized Python Interpreter instead of executing the current interactive interpreter. Click OK . Now run the script by pressing F5 . Debug script by pressing Ctrl+F5 .
+1
source

Source: https://habr.com/ru/post/910267/


All Articles