Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Errors at Python program exit: "close failed in file object destructor"; "sys.excepthook is missing"

Tags:

python

linux

sys

After the last line (print statement) in my python code, I get the following error:

close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr

Anyone know where this might be coming from?

Update: My python code is extremely long but I will post portions that may have something to do with this error:

For one, near the beginning of the process I redirect stdout and stderr to a log file like this:

so = se = open(logfile, 'w', 0)                       
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) 
os.dup2(so.fileno(), sys.stdout.fileno())           
os.dup2(se.fileno(), sys.stderr.fileno())

I do this all the time though and have never run into this error but it seems the most likely reason I'm seeing this.

like image 751
user20408 Avatar asked Dec 23 '22 20:12

user20408


1 Answers

Adding the following statement to the very end of my main function fixes this issue for me:

try:
    sys.stdout.close()
except:
    pass
try:
    sys.stderr.close()
except:
    pass
like image 136
user20408 Avatar answered Dec 31 '22 14:12

user20408