Document directory
- 2.1.1 parameter transfer
- 2.1.2 Interaction Mode
- 2.2.1 error capture
- 2.2.2 executable Python script
- 2.2.3 source file encoding
2.1 call Interpreter
The Python interpreter is usually installed in/usr/local/bin/python3.3 and put/usr/local/bin in the search path of your UNIX shell, enable startup by typing the command in shell:
python3.3
Because the interpreter installation directory is optional, other paths are also acceptable. Verify with your local Python expert or system administrator. (/Usr/local/python is a popular option)
On a Windows machine, python is usually installed in c: \ python33. You can change this location during installation. Add this directory to path. You can enter the following command at the command prompt:
set path=%path%;C:\python33
Enter a file terminator (Control-D on UNIX and Control-Z on Windows) at the primary prompt to exit the interpreter in zero state. If this is not the case, enter the quit () command to exit the interpreter.
Interpreter operations are somewhat like UNIX shell: It reads and executes commands interactively when a call is input to connect to a tty device. When a file name parameter or file is input as a standard, it reads and executes scripts from the file.
The second method to start the interpreter is Python-C command [Arg].... Execute the statement in the command, which is similar to the-C option of shell. Because Python statements often contain spaces or other characters, these are special characters for shell. We recommend that you use single quotes to include all the commands.
Some Python modules are also very useful as scripts. They can use Python-M module [Arg]... it executes the source file, just as you spell the full name in the command line.
When a script file is used, you can run the script and enter the interactive mode. You can pass in-I before the script.
2.1.1 parameter transfer
After knowing the interpreter, the Script Name and additional parameters are converted to a string list and assigned to the argv variable in the SYS module. You can run import sys to access this list. The list length must be at least 1. If no script or parameter exists, SYS. argv [0] is an empty string. When the Script Name is-(meaning standard input), SYS. argv [0] is set -. When the-C command is used, SYS. argv [0] is set to-C. When the-M module is used, SYS. argv [0] is set to the full name of the specified module. The options after-C or-m are not used by the options of the python interpreter, but are left in SYS. argv and processed by commands or modules.
2.1.2 Interaction Mode
When reading a command from a tty, the interpreter is said to be an interactive mode. It prompts the next command through the primary prompt, usually three greater than (>>> ). For the row to continue, use the prompt at the second prompt. The default value is three dots (...). Before printing the first prompt, the interpreter prints a welcome message to display the publication letter and copyright note:
$ python3.3Python 3.3 (default, Sep 24 2012, 09:25:04)[GCC 4.6.3] on linux2Type "help", "copyright", "credits" or "license" for more information.>>>
When entering a multi-row structure, you need to continue the line. For example, see 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 capture
When an error occurs, the interpreter prints an error message and stack trace. In interactive mode, return to the primary prompt. When a file is input, the stack trace is printed and exited in a non-zero state. (Exceptions are caught by the limit t clause of the try statement.) Some errors are unconditional and fatal, causing non-zero exit. This is caused by internal conflicts and insufficient memory. All error messages are written to standard error streams. Normal command output is written to standard output.
Type the interrupt character (usually control-C or del) to the primary or secondary prompt. Cancel the input and return to the primary prompt. Keyboardinterrupt exception occurs when the command is running and can be captured by try statements.
2.2.2 executable Python script
On bsd unix systems, Python scripts are executable directly, such as shell scripts. Add this line:
#! /usr/bin/env python3.3
(Assuming that the interpreter is on the user's path) at the beginning of the script, give the file an executable mode. #! It must be the first two characters of the file. On Some platforms, the first line must end with a UNIX style (\ n), not a Windows style (\ r \ n ). Note that the # character is usually used as a single line comment.
The script can be given an executable mode or permission. Run the CHMOD command:
$ chmod +x myscript.py
In Windows, there is no concept of executable mode. The pythoninstaller automatically links the. pyfile to python.exe and double-click a python file to run it as a script. The extension can also be. pyw. In this case, the appearance of the console window is generally blocked.
2.2.3 source file encoding
By default, Python source files are treated in UTF-8 encoding. Most of the world's language characters can be used as string literal, identifiers, and comments at the same time. Although the standard library uses only ASCII characters as identifiers, a lightweight code should follow the same conventions. In order for all characters to fit in display, your editor must be able to recognize the file as a UTF-8 and must use a font that supports all characters in the file.
You can also specify a different encoding for the source file. In #! Add a special comment line after the line to define the source file encoding:
# -*- coding: encoding -*-
With this Declaration, all content in the source file is treated as new encoding, and the possible encoding list is found in the python library.
For example, the editor you choose does not support UTF-8 encoding files, and you are determined to use other encoding, such as Windows-1252, you can write like this:
# -*- coding: cp-1252 -*-
All characters in the source file use the windows-1252 Character Set. This special encoding annotation must be placed in the first or second line of the file.
This article is the official website content translation, original address: http://docs.python.org/3/tutorial/interpreter.html