question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The external system terminal close after running script if the Python console exits due to an exception

See original GitHub issue

Issue Report Checklist

  • Searched the issues page for similar reports
  • Read the relevant sections of the Spyder Troubleshooting Guide and followed its advice
  • Reproduced the issue after updating with conda update spyder (or pip, if not using Anaconda)
  • Could not reproduce inside jupyter qtconsole (if console-related)
  • Tried basic troubleshooting (if a bug/error)
    • Restarted Spyder
    • Reset preferences with spyder --reset
    • Reinstalled the latest version of Anaconda
    • Tried the other applicable steps from the Troubleshooting Guide
  • Completed the Problem Description, Steps to Reproduce and Version sections below

Problem Description

Especially when testing code involving PyQt or PySide, I often like to execute my code using an external system terminal on Windows with the option Interact with the Python console after execution checked.

image

However, very often when an exception is raised during execution, the Python console and the external system terminal both close, so that it is impossible to see the traceback of the error.

Below is a minimal working example I’ve put together that allow to reproduce this behavior on my system:

import sys
from PyQt5.QtWidgets import QPushButton, QWidget, QApplication, QGridLayout


class SomeClass(QWidget):
    def __init__(self):
        super().__init__()
        layout = QGridLayout(self)
        btn_crash = QPushButton('Crash')
        btn_crash.clicked.connect(self.crash)
        btn_crash2 = QPushButton('Crash2')
        btn_crash2.clicked.connect(self.crash2)
        btn_close = QPushButton('Close')
        btn_close.clicked.connect(self.close)
        layout.addWidget(btn_crash)
        layout.addWidget(btn_crash2)
        layout.addWidget(btn_close)

    def show(self):
        super().show()

    def crash(self):
        raise ValueError

    def crash2(self):
        return self.crash2()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = SomeClass()
    window.show()
    sys.exit(app.exec_())

image

What steps reproduce the problem?

  1. Run the script in an external system terminal in Spyder with the option Interact with the Python console after execution
  2. Click on Close button
  3. Run the script again
  4. Click on Crash or Crash2 button

What is the expected output? What do you see instead?

When clicking on the Close button, the Python console and the terminal do not close and it is indeed possible to interact with the Python console after execution. When either the Crash or Crash2 button is clicked, the Python console and the external system terminal both close, so that it is impossible to see the traceback of the error.

What I have done to go around this behavior on my system is to change the way the external system terminal is started in Windows from (see here) :

cmd = 'start cmd.exe /c "cd %s && ' % wdir + ' '.join(p_args) + '"'

to (see https://stackoverflow.com/a/46882828/4481445) :

cmd = 'start cmd.exe /K "cd %s && ' % wdir + ' '.join(p_args) + '"' + ' ^&^& exit'

This prevents the external system terminal from closing when the Python console exit with an error code other than 0. If there is no error though, then the terminal is closed automatically like before.

Versions

  • Spyder version: Spyder 4.0.0.dev0
  • Python version: Python 3.7.1 64-bit
  • Qt version: Qt 5.9.6
  • PyQt version: PyQt5 5.9.2
  • Operating System name/version: Windows 10

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
CAM-Gerlachcommented, May 4, 2019

I agree, @goanpeca ; I don’t see how immediately closing the terminal window if an error occurs (thus hiding the error, and possibly even that an error occurred at all) would ever be what a user would want or expect. In the worst case, they can spend a second closing it themselves, which is nothing compared to the cost of the alternative in the most likely case.

2reactions
jnsebgosselincommented, May 6, 2019

Cool, I’ll prepare a PR then.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to stop/terminate a python script from running?
More ideally, you can do sys.exit() . sys.exit() which might terminate Python even if you are running things in parallel through the ...
Read more >
Exception Handling — Python 3.11.1 documentation
This utility function prints a warning message to sys.stderr when an exception has been set but it is impossible for the interpreter to...
Read more >
Troubleshoot Terminal launch failures - Visual Studio Code
Try running your designated integrated terminal shell outside VS Code from an external terminal or command prompt. Some terminal launch failures may be...
Read more >
How Do You End Scripts in Python? - LearnPython.com
We may want to exit Python if the script is terminated by using Ctrl + C while its running. The following block of...
Read more >
How To Use subprocess to Run External Programs in Python 3
Python 3's subprocess module can be used to run external programs and read ... an exception if a program we run exits with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found