博客
关于我
解决jupyter运行pyqt代码内核重启
阅读量:129 次
发布时间:2019-02-27

本文共 879 字,大约阅读时间需要 2 分钟。

在Jupyter Notebook或QtConsole下运行PyQt程序时,常常会遇到内核死亡的错误。经过仔细分析,问题的根源在于PyQt应用程序的主循环app.exec_()未能正确退出,导致内核被立即终止。以下是解决这个问题的详细步骤:

  • 问题分析

    • 在Jupyter或者QtConsole环境下,Python内核只能运行一个。
    • 如果PyQt程序的主循环app.exec_()未能正确退出,可能会导致内核死亡。
  • 解决方案

    • app.exec_()运行在独立的线程中,这样可以避免阻塞内核。
    • 检查运行环境,确保在Jupyter或QtConsole下使用exit(0)而不是sys.exit(),以避免终止内核。
  • 优化后的代码

  • from PyQt5.QtWidgets import *from PyQt5.QtGui import *from PyQt5.QtCore import *import sysimport threadingapp = QApplication(sys.argv)window = QWidget()window.show()def run_app():    app.exec_()thread = threading.Thread(target=run_app)thread.start()if 'jupyter' in sys.argv or '@ipython' in sys.argv:    exit(0)else:    sys.exit()
    1. 效果说明

      • 代码修改后,PyQt程序在Jupyter Notebook或QtConsole下运行时不会导致内核死亡。
      • 通过检查命令行参数,确保在Jupyter环境下使用合适的退出方式,避免不必要的错误。
    2. 注意事项

      • 确保在不同的运行环境下正确设置退出方式,以适应不同的使用场景。
      • 如果需要更详细的错误处理,可以在退出前添加必要的清理步骤。
    3. 通过以上方法,可以在不影响Jupyter Notebook内核的情况下,顺利运行和退出PyQt程序,避免了常见的内核死亡错误。

    转载地址:http://djsb.baihongyu.com/

    你可能感兴趣的文章
    Orcale表被锁
    查看>>
    svn访问报错500
    查看>>
    org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
    查看>>
    org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
    查看>>
    org.apache.poi.hssf.util.Region
    查看>>
    org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
    查看>>
    org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
    查看>>
    org.hibernate.HibernateException: Unable to get the default Bean Validation factory
    查看>>
    org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
    查看>>
    org.tinygroup.serviceprocessor-服务处理器
    查看>>
    org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
    查看>>
    org/hibernate/validator/internal/engine
    查看>>
    SQL-36 创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。
    查看>>
    ORM sqlachemy学习
    查看>>
    Ormlite数据库
    查看>>
    orm总结
    查看>>
    os.path.join、dirname、splitext、split、makedirs、getcwd、listdir、sep等的用法
    查看>>