Windows python3.2 shell environment (python is called Interpreter)

Source: Internet
Author: User
Tags exit in


[Enter the python shell environment:] (python is called the command interpreter, windows is called cmd, and unix is called shell)

Cmd Input
Set path = % path %; e: \ python2.7
Enter python

Enter the python shell Interface, as shown in the following code:
>>>

To exit the python shell Interface, press Ctrl + Z in windows and Ctrl + D in linux.
Upon entry, you will be prompted to enter basic commands such as help copyright credits license.


----------------------------------

The second method to start the interpreter is: python commands with commands/modules/script files. One of the three methods is to start the interpreter.

[Python-c command [arg]...,]
This method will execute the statements in command, which is equivalent to the-c option of Shell.
It is best to enclose the entire command in single quotes.

[Python-m module [arg]...]
Some Python modules can also be used as scripts.
Call, which is the same as running the command with its complete file name in the command line.

[Python-I interactive [arg]...]
When using a script file, the script is often run and then enters the interactive mode. this can also be achieved by adding the-I parameter before the script. (If the script is from a standard input, it cannot be executed as described above .)
----------------------------------

2.1.1. parameter transfer
When the interpreter is called, The Script Name and additional parameters are passed in a string list named sys. argv.

If no script or parameter is specified, it has at least one element: sys. argv [0]. At this time, it is an empty string,
When the Script Name is specified as '-' (indicating the 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 module.
Parameters after-c command or-m module are not intercepted by the option processing mechanism of the Python interpreter, but are left in sys. argv for commands or module operations.

2.1.2. Interaction Mode
When reading commands from tty, we call the interpreter working in * interactive mode ).
In this mode, it prompts the next command through * primary prompt * (primary prompt *). The primary prompt is usually three major signs (>>> );

When there are multiple lines of commands, the shell interface will display [from the prompt], [from the prompt] consists of three points to identify..., prompting a command to continue.
$ Python3.2
Python 3.2 (py3k, Sep 12 2011, 12:21:02)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

When you enter a multi-row structure, you need a secondary prompt,
Example: Enter the following if statement:
(After print is completed, press enter to wrap the line, and the command will be executed)

>>> 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.1. handle errors
When an error occurs, the interpreter outputs the error message and stack trace. in interactive mode, it returns to the primary prompt. If it is executed from the input file, it exits in a non-zero state after printing the stack trace. (the exception thrown in the try statement and processed by the limit t clause is not described here ). some very fatal errors may cause exit in a non-zero state, which is usually caused by internal problems or memory overflow. All error messages are written to the standard error stream; the common output executed in the command is written to the standard output.

[Enter the interrupt character (usually Control-C or DEL) at the primary or secondary prompt to cancel the current input and return to the primary prompt .]
[2] inputting an interrupt character during Command Execution throws a KeyboardInterrupt exception, which can be intercepted by the try statement.


2.2.2. executable Python script
Similar to the bsd unix system, Python scripts can be directly executed as Shell scripts,
You only need to add a line of text at the beginning of the script file to declare the mode:

#! /Usr/bin/env python3.2

(Check that the Python interpreter exists in your PATH environment variable ).

#! These two characters must be the first two characters of the file. on Some platforms, the first line must end with a UNIX-style line terminator ('\ n') and cannot use a Windows (' \ r \ n') line terminator. note: ''' # ''' is used to start a line of Python comments.


The script can use the chmod command to specify the executable mode or permission:

$ Chmod + x myscript. py


In Windows, there is no concept of "executable mode,
Therefore, the Python installer automatically binds the file with the. py suffix to python.exe,
Therefore, double-click a Python file and run it as a script.

The extension can also be. pyw, And the workbench window will be hidden without being opened.
]

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.