Reproduced Reason why the command cannot be executed properly after the Python IDLE reload (SYS)

Source: Internet
Author: User

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=""&#39;<stdin= ">", Mode ' R ' at 0x0000000001cab030>>>>PrintSys.stdout<idlelib.pyshell.pseudooutputfile object=""at=""0x00000000027af320="">>>>PrintSys.stderr<open file=""&#39;<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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.