Python Guide - Using Python Interpreter

zhaozj2021-02-11  199

2. Using the Python Interpreter

2.1 call interpreter

Usually Python's interpreter is installed in the / usr / local / bin / python directory of the target machine; put the / usr / local / bin directory into your UNIX shell's search path, make sure it can be entered

Python

Come start. Because the installation path is optional, it is also possible to install other locations, you can contact users or system administrators installing Python. (For example, / usr / local / python is a very common choice)

Enter a file end value (Unix is ​​Ctrl D, WindWOS is a CTRL Z) interpreter exits 0 value (that is, there is no error, normal exit-translator). If this doesn't work, you can enter the following command to exit: "import sys.exit ()".

The line editing function of the interpreter is not very complicated. The interpreter installed on UNIX may have a GNU Readline library support, so you can give additional interactive editing and history. The most convenient way to check the command line editor support is to enter Ctrl P at the primary prompt. If there is a beep (computer speaker), you can use the command line editing function, you can find the shortcut key from Appendix A. If there is no sound, or P is displayed, indicating that the command line editing function is not available, you only use the retractable to delete the input command.

The interpreter operates Some like UNIX Shell: When using a terminal device as a standard input to call it, interpretation and execution commands of the interpreter interactively, when the file name parameter is used as a standard input device, it is interpreted from the file and Execute the script.

The third method of the start interpreter is "Python -c Command [Arg] ...", this method can perform the statement directly in the command line, equivalent to the SHELL-C option. Because the Python statement usually includes special characters such as spaces, it is best to pack the entire statement with double quotes.

Note "Python File" and "Python

"It is different. For the latter case, the program is similar to the input requests such as invut () and RAW_INPUT () from the determined file. Because the file has been fully read before the parser starts execution, so The program points to the file end. In the former case (this is usually what you need) they are from any standard input to the Python interpreter, whether they are files or other devices.

When using a script file, you often run the script and then enter your interactive mode. This can also be implemented by adding the -i parameter before the script. (If the script comes from standard input, it is not possible to run, the same as the previous parameter.) 2.1.1 Parameter passes the interpreter, the script name and additional parameters are incorporated into a string list named Sys.Argv. When there is no script and parameters, it also has at least one element: sys.argv [0] is an empty string. When the scripn name is specified as '-' (indicating the standard input), sys.argv [0] is set to '-', when using the -c command, sys.argv [0] is set to '-C'. The parameter after the -c command will be intercepted by the Python interpreter's option processing mechanism, but stay in sys.argv for scripting command operations. 2.1.2 Interactive mode When you read the command from the TTY, we call the interpreter to operate in interactive mode. In this mode it is executed according to the primary prompt, the main prompt usually identifies three larger than the number (">>"); the continued portion is called the slave prompt, from three points ("... ... "). Prior to the first line, the interpreter prints welcome information, version number and authorization tips: PythonPython 2.3 (# 1, Jul 30 2003, 23:22:59)

[GCC 3.2 20020927 (prenderase)] on cygwin

Type "Help", "Copyright", "Credits" or "license" for more information.

>>> When entering multiple lines, you need a slave prompt, for example, the following IF statement: >>> the_world_is_flat = 1

>>> IF the_world_is_flat:

... Print "Be Careful Not to Fall Off!"

...

BE CAREFUL NOT To Fall Off! 2.2 Interpreter and its environment 2.2.1 Error handling When there is an error, the interpreter prints an error message and stack tracking (monitor) . In interactive mode, it returns to the main prompt if it is executed from the file input, which exits in a non-zero state after the print stack tracker. (Exception can be controlled by the Except clause in the TRY statement, so that the error message above will not appear) Some very fatal errors cause to exit in non-zero state, which is usually caused by internal contradictions and memory overflows. All error messages are written to a standard error stream; the ordinary output written in the command is written. In the primary prompt or an ensemble, enter the intermission (usually Control-C or DEL) will cancel the current input and return to the primary command line. 2.1 Enter a interrupt when performing a command will throw a keyboardInterRupt exception, which can be cut by the TRY sentence. 2.2.2 Executing the Python Script BSD system, the Python script can be executed directly like the shell script, as long as you write a line command in the script file, specify file and mode: #! / Usr / bin / env python (Use User Path Notification Interpreter "#!" Must be the first two characters of the file, on some platforms, the first line must end with the Unix style end of the line ("/ n"), cannot use Mac ("/ R") or Windows ("/ R / n") endorse. Note that "#" is the starting fractal of Python is a row comment. The script can specify execution mode and permissions via the chmod command. $ chmod x myscript.py 2.2.3 Source program encoding Python's source file can be encoded using a character set other than ASCII. The best practice is to define the character set with a special annotation line behind the #! # - * - CODING: ISO-8859-1 - * - According to this statement, Python will convert the characters in the file to Unicode, in this case, this character set is ISO-8859-1 . In the Python Library Reference Manual, you can find a list of available codes (according to my experiment, Chinese seems to only use CP-936 or UTF-8, not directly support GB, GBK, GB-18030 or ISO-10646 - Translator's Note ). If your file editor supports the UTF-8 format, you can save the UTF-8 tag (AKA BOM - BYTE ORDER MARK), you can use this instead of the coding declaration (it seems that at least Jext does not support this, and Vim, I haven't found its code to set it, or I am old, and I've specified the encoding of the source code - Translator's Note). IDLE can support it by setting Options / General / Default Source Encoding / UTF-8. It should be noted that the old Python does not support this tag (Python 2.2 or earlier), it also cannot support the operating system to support #! File. Using UTF-8 internal code (whether with markers or coding declarations), we can use most of the world in strings and comments. Non-ASCII character sets cannot be used in the identifier. In order to display all characters correctly, you must save the file as a UTF-8 format in the editor, and use the font of all characters in the file. 2.2.4 When the startup file of the interactive environment uses the Python interpreter, we may need to execute some commands at each interpreter.

You can include the command you want to perform in a file, set a environment variable named PythonStartup to specify this file. This is similar to the .profile file of UNIX shell. This file is read-only in the interactive session period. When Python interprets the file from the script or as a terminal as an external command source (although their behavior is very like being in the interactive session period.) Its and interpreters The executed command is in the same namespace, so everything it defines or references can be used in unrestricted uses in the interpreter. You can also change SYS.PS1 and SYS.PS2 instructions in this file. If you want to perform an additional startup file in the current directory, you can add the following code: "if os.path.isfile ('. Pythonrc.py'): execfile ('. Pythonrc.py '). If you want to use the startup file in a script, you must write such a statement in the script: import OSFileName = Os.Environ.get ('PythonStartup')

IF filename and os.path.isfile (filename):

EXECFILE (filename)

footnote

... Note:

2.1

An error in the GNU Readline package may result in normally.

转载请注明原文地址:https://www.9cbs.com/read-4566.html

New Post(0)