Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: Process finished with exit code 1 when using PyCharm and PyQt5

I have three Python(3.4.3) scripts. One of them is for controlling the .ui file generated by PyQt5. When I run the GUI program it accepts all the data and everything and when I press the OK button on an InputDialog the window closes and the console displays.

Process finished with exit code 1

When I run the same code on Python IDLE, it shows:

<<<<<<RESTART>>>>>>

This never happenned when I used this same Python(3.4.3 or 2.7) code on Visual Studio. What could be the reason?

Here is the code of the python file controlling the .ui file.

import sys
from PyQt5 import QtCore, QtGui, uic, QtWidgets
from Email import encrypt_email
from Email import decrypt_email
from Email import newuser

qtCreatorFile = "rsegui.ui" # Enter file here.

Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)

class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        user, ok = QtWidgets.QInputDialog.getText(self, 'New User', 
    'Are you a new user?')
        user=str(user)
        if user in "YESYesyesYy":
            email, ok = QtWidgets.QInputDialog.getText(self, 'New User', 
    'Enter Your Email ID:')
            email1=str(email)
            self.sender.setText(email)
            newuser(email1)

    self.encrypt_and_send.clicked.connect(self.EncryptEmail)
    self.decrypt.clicked.connect(self.DecryptEmail)
    self.clear.clicked.connect(self.ClearEncrypt)
    self.clear_2.clicked.connect(self.ClearDecrypt)
    self.sender.setPlaceholderText("Your Email ID")
    self.receiver.setPlaceholderText("Receivers, Separate them by ';'")
    self.subject.setPlaceholderText("Enter Subject")
    self.message.setPlaceholderText("Enter Message")
    self.sender_2.setPlaceholderText("Your Email ID")
    self.message_2.setPlaceholderText("Encrypted Text")



    def EncryptEmail(self):
       sender = str(self.sender.text())
       receiver = str(self.receiver.text())
       receivers = receiver.split(';')
       subject = str(self.subject.text())
       message = str(self.message.text())
       password, ok = QtWidgets.QInputDialog.getText(self, 'Password', 
'Enter your password:',QtWidgets.QLineEdit.Password)
       encrypt_email(sender,receivers,subject,message,password)

    def DecryptEmail(self):
       email = str(self.sender_2.text())
       message = str(self.message_2.text())
       self.decrypted.setText(decrypt_email(email,message))

    def ClearDecrypt(self):
       self.sender_2.clear()
       self.message_2.clear()

    def ClearEncrypt(self):
       self.sender.clear()
       self.message.clear()
       self.receiver.clear()
       self.subject.clear()

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())
like image 613
daipayan Avatar asked Dec 18 '15 20:12

daipayan


People also ask

Why does Pycharm keep saying process finished with exit code 0?

Solution: It means that there is no error with your code. You have run it right through and there is nothing wrong with it. Pycharm returns 0 when it has found no errors (plus any output you give it) and returns 1 as well as an error message when it encounters errors.

What does process finished with exit code 1 mean in Python?

exit code (1) means there was some issue which caused the program to exit. For example if your program is running on port :8080 and that port is currently in used or not closed, then you code ends up with exit code 1.

How do I exit Pycharm code?

That isn't specific to pycharm or python. In program there is no need to invoke exit function explicitly when it runs success it invoke exit(0) by default, invoke exit(not_zero_num) when runs failed. You can also invoke exit function with different code(num) for analysis.


2 Answers

I had the same problem in pycharm, python 3.8, qt5. The stacktrace was never shown for qt errors inside pycharm; running the file from cmd the error was shown correctly instead.

I solved by doing the following: open Edit Configurations of the file you want to run, scroll down and check the box Emulate terminal in output console.

like image 106
Wippo Avatar answered Oct 06 '22 13:10

Wippo


I have dealt with the same problem, and the answer is twofold:

  1. The reason it's crashing could be any number of things. It's probably a programming bug, calling a function that doesn't exist, passing a widget instead of a layout, etc. But since you're not getting useful output you don't know where to look for the culprit. This is caused by:
  2. PyQT raises and catches exceptions, but doesn't pass them along. Instead it just exits with a status of 1 to show an exception was caught.

To catch the exceptions, you need to overwrite the sys exception handler:

# Back up the reference to the exceptionhook
sys._excepthook = sys.excepthook

def my_exception_hook(exctype, value, traceback):
    # Print the error and traceback
    print(exctype, value, traceback)
    # Call the normal Exception hook after
    sys._excepthook(exctype, value, traceback)
    sys.exit(1)

# Set the exception hook to our wrapping function
sys.excepthook = my_exception_hook

Then in your execution code, wrap it in a try/catch.

try:
    sys.exit(app.exec_())
except:
    print("Exiting")
like image 25
IronSean Avatar answered Oct 06 '22 13:10

IronSean