Most people usually execute the reload (SYS) statement just to be able to modify the Python default character set, which is the ability to call sys.setdefaultencoding (). However, if you execute reload (SYS)in idle, you will cause the next failure to execute any commands.
At first there was nothing to do with the problem, and later on StackOverflow it was seen that someone had said it. This is because idle, as a GUI shell environment, sets specific standard input, standard output, and standard error output during startup initialization, so that both the input and output are in the idle GUI shell and can be viewed in idle in the following way:
>>>ImportSYS>>>PrintSys.stdin<idlelib.pyshell.pseudoinputfile object=""at=""0x00000000027af2e8="">>>>PrintSys.stdout<idlelib.pyshell.pseudooutputfile object=""at=""0x00000000027af320="">>>>PrintSys.stderr<idlelib.pyshell.pseudooutputfile object=""at=""0x00000000027af358="">>>> </idlelib.pyshell.pseudooutputfile></idlelib.pyshell.pseudooutputfile></ Idlelib.pyshell.pseudoinputfile>
If reload (SYS) is executed manually, these three variables of thesys module will be reset, causing the output to not be displayed in idle. So the solution is very simple, just need to copy these three variables before reload ,reload and then restore back to the line:
>>> stdi,stdo,stde=sys.stdin,sys.stdout,sys.stderr>>> Reload (SYS)>> > sys.stdin,sys.stdout,sys.stderr=stdi,stdo,stdeprint sys.stdout< Idlelib.pyshell.pseudooutputfile object="" at= "" 0x00000000027af320="" >> >> </idlelib.pyshell.pseudooutputfile>
A careful person might think that afterReload (SYS) , if the standard input, standard output, and standard error output do not work, then the sentence behind relaod (SYS) sys.stdin,sys.stdout, How can sys.stderr=stdi,stdo,stde execute it? Therefore, after reload (SYS) , before the recovery, the standard input can still work properly, can be checked by the following code:
>>>Reload (SYS)>>> sys.stdout=Stdo>>>PrintSys.stdin<open file=""'<stdin= ">", Mode ' R ' at 0x0000000001cab030>>>>PrintSys.stdout<idlelib.pyshell.pseudooutputfile object=""at=""0x00000000027af320="">>>>PrintSys.stderr<open file=""'<stderr= ">", Mode ' W ' at 0x0000000001cab150>>>> </open></idlelib.pyshell.pseudooutputfile></open>
So the problem is solved, but it has to be stressed:
Never use reload (SYS) easily unless you completely erase the results! It is unwise to execute reload (SYS) just to be able to re-set Python's default encoding!
The Python designer intentionally deletes the setdefaultencoding () method inside the sys module After the python initialization is complete, in order not to change the Python default encoding at run time. To avoid some unknown problems. In fact, there are other, more reliable ways to solve the problem of coding.
Reprinted from: Reason why the command cannot be executed properly after the Python IDLE reload (SYS)-Python Development Technical Article _ Tutorial-Red and Black Alliance http://www.2cto.com/kf/201411/355112.html
Reproduced Reason why the command cannot be executed properly after the Python IDLE reload (SYS)