[PyQt] PyQt5 and Spyder 3.3.2 Conflict, Beautiful Soup, <An error occurred while starting the kernel>
Kyle Altendorf
sda at fstab.net
Sun Mar 24 01:58:40 GMT 2019
Have you verified in those cases that the app exit is being called? If you fail out because of an exception it won't be. I would really manage the app outside your class. (Does Qt really force you to inherit here?). Then you can try/finally to make sure it gets exited even after a python exception. Or even make yourself a context manager.
Cheers,
-kyle
On March 23, 2019 9:28:13 PM EDT, Gene Ensor <g_ensor at hotmail.com> wrote:
>Team,
>
>Per
>
>https://github.com/spyder-ide/spyder/wiki/How-to-run-PyQt-applications-within-Spyder
>
>I believe I have the following problem:
>
>"The most common problem when running a PyQt multiple times inside
>Spyder is that a QApplication instance remains in the namespace of the
>IPython console kernel after the first run. In other words, when you
>try to re-run your application, you already have a QApplication
>instance initialized."
>
>Hence when ran a second time I get "An error occurred while starting
>the kernel". However I am not skilled enough to update the below code
>to behave correctly my environment.
>
>Anaconda "Python 3.7.1 64-bit | Qt 5.9.6 | PyQt5 5.9.2 | Windows 10 |
>Spyder 3.3.2
>
>Can you assist? Thanks!
>
>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>import sys
>from PyQt5.QtWebEngineWidgets import QWebEnginePage
>from PyQt5.QtWidgets import QApplication, QDialog
>from PyQt5.QtCore import QUrl, QCoreApplication
>
>import bs4 as bs
>import urllib.request
>
>
>class Page(QWebEnginePage):
> def __init__(self, url):
># PyQt5 has the "minimal" platform plugin.
># To use it, modify the argv passed to QApplication to include
># ['-platform', 'minimal'].
> self.app = QApplication(sys.argv)
> QWebEnginePage.__init__(self)
> self.html = ''
> self.loadFinished.connect(self._on_load_finished)
> self.load(QUrl(url))
> self.app.exec_()
>
> def _on_load_finished(self):
> self.html = self.toHtml(self.Callable)
> print('Load finished')
>
> def Callable(self, html_str):
> self.html = html_str
> self.app.quit()
>
>
>def main():
> page = Page('https://pythonprogramming.net/parsememcparseface/')
> soup = bs.BeautifulSoup(page.html, 'html.parser')
> js_test = soup.find('p', class_='jstest')
> print(js_test.text) #fixed
> # output should be "Look at you shinin!"
>if __name__ == '__main__': main()
>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
More information about the PyQt
mailing list