How to pronounce IDLE Python Interpreter

2.Using the Python interpreter¶

2.1. Calling the interpreter¶

If the Python interpreter is installed on a computer, it can usually be found under. If you put in the search path of the Unix shell, you can call the interpreter with [1]:

The selection of the installation path for the interpreter is an installation option so that it can also be installed elsewhere. This has to be clarified with the local Python guru or the system administrator. (A popular alternative is for example)

On Windows computers, the Python installation is usually located under, even if this can be changed during the installation process. To add this directory to the search path, you can enter the following command in the DOS prompt:

setpath =% path%; C: \ python33

By entering an end-of-file character (EOF; under Unix, under Windows) in the interpreter's prompt, the interpreter is terminated with the return value zero. If it doesn't, the interpreter can be terminated with the following command line:.

The interpreter's options for editing the input are quite limited, but can be expanded using the GNU readline library. You can check whether these advanced options are available by typing a into the command prompt. If it beeps, “readline” support is available. In this case you will find an introduction to the individual keys in the appendix Interactive input processing and replacement of the course. If no beep is heard or appears, there is no “readline” support and the only way to edit is to use the backspace key to remove characters in the current input line.

Basically, the interpreter can be operated in a similar way to a Unix shell: If it is called with a tty device as standard input, it reads and executes commands interactively. When called with a file name as an argument or with a file as standard input, it reads and introduces it script from this file.

A second way to start the Python interpreter is by creating the statement (s) in this command are executed, analogous to the shell option. Since Python statements often contain spaces or other characters that are specially treated by the shell, the complete command put in single quotes.

Some Python modules are also useful as scripts and can be called with. This will make the source code of module executed as if you had entered the full name on the command line.

Warning: there is a difference between and! In the second case, input requests from the program, such as calling file done. Since this file has already been read to the end by the parser before execution begins, the program immediately encounters an end-of-file. In the first case, what you would normally expect happens: The input requests are handled by the file or device that is available as standard input.

When using a script file, it is often useful to run the script and then switch to interactive mode. You can do this with the option in front of the script.

2.1.1. Passing arguments¶

If a script name and additional arguments are passed to the interpreter, then these are converted into a list of character strings and passed to the variable in the module. You can access it using. If no script and no arguments were passed, then is an empty string. If the script name is specified as (which is standard input), then is set to. Becomes command is used, then gets the value, if using module the full name of the module found. Options after command or module are not processed by the Python interpreter, but are passed to as values.

2.1.2. Interactive mode¶

When commands are read from a tty (usually this will be a console), one speaks of a interactive mode of the interpreter. In this mode the interpreter waits with the primary prompt, which usually consists of three greater-than characters (), on user input. After continuation lines the interpreter shows the secondary prompt, that's usually three dots (). In addition, after starting, the interpreter initially displays a brief information text that contains, among other things, the version number of the interpreter and a note on copyright.

$ python3.3 Python 3.3 (py3k, Apr 1 2010, 13:37:42) [GCC 4.4.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>

Continuation lines result when multi-line constructs are entered, such as the following statement:

>>> the_world_is_flat = True >>> ifthe_world_is_flat: ... print ("Be careful not to fall off!") ... Be careful not to fall off!

2.2. The interpreter and its environment¶

2.2.1. Error handling¶

If an error occurs, the interpreter shows an error message with a progress report (Stack trace) at. In interactive mode, it will then return to the primary prompt. If the input came from a file, it exits after the error report has been output with a non-zero return value. Exceptions that are processed in a block are not regarded as exceptions in this context. Some errors lead to an immediate abort of the interpreter with a return value not equal to zero. This applies, for example, to internal inconsistencies or insufficient memory. All error messages are written to the standard error output stream; ordinary output from executed commands is written to standard output.

Entering the interrupt character (usually or DEL) at the primary or secondary prompt will cancel the entry and return to the primary prompt. [2] An interrupt during an instruction execution causes an exception that can be handled by an instruction.

2.2.2. Executable Python scripts¶

On BSD-like Unix systems, a Python script - similar to a shell script - can be made directly executable by writing the following line (shebang) at the beginning of the script

It is assumed that the path to the interpreter is in the user's. They have to be the first two characters of the file. On some platforms, this first line must end with a Unix line ending () and not a Windows line ending (). Note: The diamond is used in Python to start a comment.

Such a script can then be given execution rights with the help of the command chmod be awarded:

The term “execution rights” does not exist on Windows systems. The Python installer automatically associates files with, so double-clicking a Python file will run it as a script. The filename extension can also be, in which case the console window that normally appears is suppressed.

2.2.3. Coding of source code¶

By default, Python source code files are treated as encoded in UTF-8. In this encoding, the characters of most languages ​​can be used simultaneously in string literals, identifiers and comments. However, the standard library only uses ASCII characters for identifiers - a convention that all portable code should follow. In order to display all these characters correctly, an editor must recognize that the file is UTF-8 encoded and use a font that supports all characters in the file.

If you want to use a coding other than UTF-8 for a source text file, a further, special comment line must be inserted immediately below the line, which defines the coding

# - * - coding: Coding - * -

With this specification, everything in the source text file is treated as if it had the encoding Coding instead of UTF-8. The list of possible encodings can be found in the Python Library Reference, in the section on.

For example, if an editor does not support UTF-8 encoded files and insists on using a different encoding, let's say Windows-1252, you can use the following encoding line

# - * - coding: cp-1252 - * -

still use all characters of the Windows-1252 character set in the source code. This special coding comment must be in the first or second Line of the file.

2.2.4. The interactive startup file¶

When using Python interactively, it is sometimes helpful to automatically execute a few standard commands each time the interpreter is started. This can be done by creating an environment variable named that points to a file that contains the startup commands. This is similar to the Unixshells file.

This file is only read in interactive sessions. If the interpreter executes a script or is explicitly specified as the source - which otherwise corresponds to an interactive session - the startup file is ignored. It is executed in the same namespace as interactive commands, so that objects that are defined or imported in the startup file can be used in the interactive session without qualification. The prompts and can also be set in this file.

If further startup files are to be read from the current directory, this can be achieved using code as in the global file. If the startup file is to be used in a script, this must be done explicitly in this script:

importosfilename = os.environ.get ('PYTHONSTARTUP') iffilenameandos.path.isfile (filename): exec (open (filename) .read ())

2.2.5. The customization modules¶

Python offers two hooks to customize it: and. To try it out, you'll first need to find out the location of your user folder. Start Python and enter this:

>>> importsite >>> site.getusersitepackages () '/ home / user / .local / lib / python3.2 / site-packages'

There you can create a file named and enter everything you want. It will affect every call to Python unless the call includes the option to prevent automatic import.

works the same way, but it is typically created and pre-imported by an administrator in the global folder. More on this in the documentation of the module.

Footnotes


© Copyright 2010 - 2013, Michael Markert et al. Revision.

Built with Sphinx using a theme provided by Read the Docs.