简体   繁体   中英

Python script suddenly exiting when being run through windows

I'm calling a Python script from another program (Stata), which has a command for calling the operating system (shell/winexec). This, in turn, opens a system window where you can see the Python script running. Sometimes the script runs just fine and sometimes it abruptly quits before finishing. I have no idea why. I never have this problem when I run the script in IDLE. I'm using Windows and Python 3.4.

I've tried catching the error by logging the Python script, but no error ever gets recorded there. Everything seems to be working normally until the program just quits (and not always at the same point). Also, I'm calling 7 scripts and only two of them ever have this sudden quitting problem (again, they always run fine through IDLE). I tried changing the order in which I call the scripts and using shell instead of winexec and vise-versa. At this point, I don't even know what to try. Is it the operating system? Python? Stata? Any ideas would be appreciated!

Couldn't have figured this out without Martineau! Posting answer in case anyone has a similar (seemingly) mysterious error.

First, I logged the error, as Martineau suggested:

import logging

logging.basicConfig(filename='C:/MyLog.log',level=logging.DEBUG)

try:
    [all my code]

except Exception as e:
    logging.info(e)

This recorded the error: "charmap' codec can't encode character '\–' in position 448: character maps to undefined". For some reason that's beyond my pay grade, IDLE and python.exe (in Python 3.4) don't handle encoding in the same way. That was causing the problem (and only when special characters were appearing, which is why it didn't break down every time).

I looked for the solution to the encoding problem and found it here . Basically, I upgraded from Python 3.4 to Python 3.6, and the encoding problem disappeared.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM