Detailed description of the role of the Python Interpreter

Source: Internet
Author: User

When using the Python interpreter, we may need to execute some commands every time the interpreter starts. You can include the commands you want to execute in a file, the interpreter can interpret and execute commands through file name parameters or input a device using a file as a standard.

The location of the Python interpreter is an installation option, so it may be elsewhere. Ask your Python experts or system administrators if necessary (/usr/local/python is another common installation path ). In Windows, Python is usually installed in C: \ Python24 [U1], but you can change this path during installation. To add this path to your path, run the following command in the command line window:

Entering a file terminator (Control-D on Unix and Control-Z on Windows) at the Python prompt will cause the interpreter to exit and return the status code 0. If the file Terminator does not work, run the "import sys; sys. quit ()" command to exit the interpreter. The row editing function of the interpreter is usually not very powerful. However, after the interpreter is installed on Unix, the support for the GNU readline library is activated. This library includes many complex interactive editing and history functions.

The fastest way to check whether the command line editing is supported is to enter Control-P at the first Python prompt you encounter. If you hear a beep, you can use the command line to edit it. You can see an introduction to these editing keys in Appendix. If nothing happens or the letter P is displayed, the command line editing function is unavailable. You can only use backspace to delete characters from the current row.

The interpreter is somewhat like a Unix Shell: when the standard input for a call is connected to a tty device, the interpreter reads and runs commands interactively. If a file name is followed as a parameter or a file is input as a standard, the file is read and the script is executed.

The second method to start the interpreter is "python-c command [arg]…". The interpreter will execute the passed command, just like the-c option of shell. Because Python statements often contain spaces or other characters that have special meanings to shell, it is best to use double quotation marks to cause the command. Some Python modules are also useful scripts. You can use "python-m module [arg]…" . The interpreter executes the source file of the module, just as you have given the full name of the module on the command line.

Note that "python file" is different from "python <file. In the latter method, the input required by the program, such as input () and raw_input (), is read from the input file. At the same time, the interpreter has read the end of the input file before the program execution.

Therefore, the program will immediately encounter an end-of-file. In the previous case (usually what you want), input is read from any file or device associated with the standard input in the Python interpreter. When using a script file, you sometimes need to enter the interaction mode after the script is executed. You can add-I before the script to achieve this effect. (This method does not work when the script is read from the standard input, as explained in the previous section ).

The Script Name and additional parameters are placed in sys. argv to input the script. Sys. argv is a string list with a minimum length of 1. When no script or parameter is input, sys. argv [0] is an empty string. When the Script Name uses '-' to indicate the standard input, sys. argv [0] is set '-'. When the-m module option is used, sys. argv [0] is set to the full name of the module.

The options after-c command or-m module are not processed by the Python interpreter, but are passed to the command or module to be executed as sys. argv.
When reading commands from tty, the interpreter is in interactive mode.

In this mode, it prompts the user to enter the next command at the primary prompt. The main prompt is usually three major signs (">>> "). When the input in a row is not terminated and the input in the next row needs to be continued, the sub-prompt will save three periods ("…").

  • Research on the Python Module
  • Correct explanation of Python object Conversion
  • Tips for learning Python extension modules
  • Analysis and Interpretation of Python applications
  • How to Implement Python configuration?

The interpreter outputs a welcome message containing the version number and copyright statement before outputting the first prompt. Once an error occurs, the interpreter prints the error message and the current stack. In interactive mode, the system returns to the primary prompt. When the input is from a file, the interpreter exits after printing the stack information and returns a non-zero error code.

(The exception caught by the explain T statement is not an error here ). Some errors are unconditional and fatal, which may lead to exit with a non-zero error code, usually caused by internal inconsistencies and memory depletion. All error messages are written to the standard error stream, and the normal output in the command is written to the standard output.

Entering the interrupt character (usually Control-C or DEL) at the primary or secondary prompt will cancel the input and return to the primary prompt. A KeyboardInterrupt exception occurs when a command is being executed and an interrupt is entered. This exception can be captured by the try statement.

  1. Introduction to Python system files
  2. How to correctly use Python Functions
  3. Detailed introduction and analysis of Python build tools
  4. Advantages of Python in PythonAndroid
  5. How to Use the Python module to parse the configuration file?

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.