Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
471 views
in Technique[技术] by (71.8m points)

python - PySide Qt script doesn't launch from Spyder but works from shell

I have a weird bug in my project that uses PySide for its Qt GUI, and in response I'm trying to test with simpler code that sets up the environment.

Here is the code I am testing with: https://stackoverflow.com/a/6906552/130164

When I launch that from my shell (python test.py), it works perfectly. However, when I run that 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 it 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 that code work when launched from my shell but not from Spyder?


Update: Mata answered that the problem happens because Spyder uses Qt, which makes sense. For now, I've set up execution in Spyder using the "Execute in an external system terminal" option, which doesn't cause errors but doesn't allow debugging, either. Does Spyder have any built-in workarounds to this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As Spyder also is a Qt application, it starts it's own QApplication. In the same process only one QApplication can exist, that's why you get the first error.

Sypder also uses matplotlib, and probably, therfore it already will have imported some of the mentioned modules, so that's why you get the second error.

So when usin it like that, you can't create your own QApplication or call matplotlib.use(). Or maybe it will work if you wrap these calls in try/except.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...