<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <link rel="STYLESHEET" href="tut.css" type='text/css' /> <link rel="SHORTCUT ICON" href="../icons/pyfav.gif" /> <link rel='start' href='../index.html' title='Python Documentation Index' /> <link rel="first" href="tut.html" title='Python Tutorial' /> <link rel='contents' href='node2.html' title="Contents" /> <link rel='index' href='genindex.html' title='Index' /> <link rel='last' href='about.html' title='About this document...' /> <link rel='help' href='about.html' title='About this document...' /> <LINK rel="next" href="node5.html"> <LINK rel="prev" href="node3.html"> <LINK rel="parent" href="tut.html"> <LINK rel="next" href="node5.html"> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta name='aesop' content='information' /> <META name="description" content=". Using the Python Interpreter "> <META name="keywords" content="tut"> <META name="resource-type" content="document"> <META name="distribution" content="global"> <title>2. Using the Python Interpreter </title> </head> <body> <DIV CLASS="navigation"> <div id='top-navigation-panel'> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td class='online-navigation'><a rel="prev" title="1. Whetting Your Appetite" href="node3.html"><img src='../icons/previous.png' border='0' height='32' alt='Previous Page' width='32' /></A></td> <td class='online-navigation'><a rel="parent" title="Python Tutorial" href="tut.html"><img src='../icons/up.png' border='0' height='32' alt='Up One Level' width='32' /></A></td> <td class='online-navigation'><a rel="next" title="3. An Informal Introduction" href="node5.html"><img src='../icons/next.png' border='0' height='32' alt='Next Page' width='32' /></A></td> <td align="center" width="100%">Python Tutorial</td> <td class='online-navigation'><a rel="contents" title="Table of Contents" href="node2.html"><img src='../icons/contents.png' border='0' height='32' alt='Contents' width='32' /></A></td> <td class='online-navigation'><img src='../icons/blank.png' border='0' height='32' alt='' width='32' /></td> <td class='online-navigation'><a rel="index" title="Index" href="node18.html"><img src='../icons/index.png' border='0' height='32' alt='Index' width='32' /></A></td> </tr></table> <div class='online-navigation'> <b class="navlabel">Previous:</b> <a class="sectref" rel="prev" href="node3.html">1. Whetting Your Appetite</A> <b class="navlabel">Up:</b> <a class="sectref" rel="parent" href="tut.html">Python Tutorial</A> <b class="navlabel">Next:</b> <a class="sectref" rel="next" href="node5.html">3. An Informal Introduction</A> </div> <hr /></div> </DIV> <!--End of Navigation Panel--> <div class='online-navigation'> <!--Table of Child-Links--> <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> <UL CLASS="ChildLinks"> <LI><A href="node4.html#SECTION004100000000000000000">2.1 Invoking the Interpreter</a> <UL> <LI><A href="node4.html#SECTION004110000000000000000">2.1.1 Argument Passing</a> <LI><A href="node4.html#SECTION004120000000000000000">2.1.2 Interactive Mode</a> </ul> <LI><A href="node4.html#SECTION004200000000000000000">2.2 The Interpreter and Its Environment</a> <UL> <LI><A href="node4.html#SECTION004210000000000000000">2.2.1 Error Handling</a> <LI><A href="node4.html#SECTION004220000000000000000">2.2.2 Executable Python Scripts</a> <LI><A href="node4.html#SECTION004230000000000000000">2.2.3 Source Code Encoding</a> <LI><A href="node4.html#SECTION004240000000000000000">2.2.4 The Interactive Startup File</a> </ul></ul> <!--End of Table of Child-Links--> </div> <HR> <H1><A NAME="SECTION004000000000000000000"><!--x--></A><A NAME="using"><!--z--></A> <BR> 2. Using the Python Interpreter </H1> <P> <H1><A NAME="SECTION004100000000000000000"><!--x--></A><A NAME="invoking"><!--z--></A> <BR> 2.1 Invoking the Interpreter </H1> <P> The Python interpreter is usually installed as <span class="file">/usr/bin/python</span> on those machines where it is available; putting <span class="file">/usr/local/bin</span> in your <font style="font-variant: small-caps;">Unix</font> shell's search path makes it possible to start it by typing the command <P> <div class="verbatim"><pre> python </pre></div> <P> to the shell. Since the choice of the directory where the interpreter lives is an installation option, other places are possible; check with your local Python guru or system administrator. (E.g., <span class="file">/usr/local/python</span> is a popular alternative location.) <P> Typing an end-of-file character (<kbd>Control-D</kbd> on <font style="font-variant: small-caps;">Unix</font>, <kbd>Control-Z</kbd> on Windows) at the primary prompt causes the interpreter to exit with a zero exit status. If that doesn't work, you can exit the interpreter by typing the following commands: "<tt class="samp">import sys; sys.exit()</tt>". <P> The interpreter's line-editing features usually aren't very sophisticated. On <font style="font-variant: small-caps;">Unix</font>, whoever installed the interpreter may have enabled support for the GNU readline library, which adds more elaborate interactive editing and history features. Perhaps the quickest check to see whether command line editing is supported is typing Control-P to the first Python prompt you get. If it beeps, you have command line editing; see Appendix <A HREF="node14.html#interacting">A</A> for an introduction to the keys. If nothing appears to happen, or if <code>P</code> is echoed, command line editing isn't available; you'll only be able to use backspace to remove characters from the current line. <P> The interpreter operates somewhat like the <font style="font-variant: small-caps;">Unix</font> shell: when called with standard input connected to a tty device, it reads and executes commands interactively; when called with a file name argument or with a file as standard input, it reads and executes a <i>script</i> from that file. <P> A second way of starting the interpreter is "<tt class="samp"><b class="program">python</b> <b class="programopt">-c</b> <var>command</var> [arg] ...</tt>", which executes the statement(s) in <var>command</var>, analogous to the shell's <b class="programopt">-c</b> option. Since Python statements often contain spaces or other characters that are special to the shell, it is best to quote <var>command</var> in its entirety with double quotes. <P> Note that there is a difference between "<tt class="samp">python file</tt>" and "<tt class="samp">python <file</tt>". In the latter case, input requests from the program, such as calls to <tt class="function">input()</tt> and <tt class="function">raw_input()</tt>, are satisfied from <i>file</i>. Since this file has already been read until the end by the parser before the program starts executing, the program will encounter end-of-file immediately. In the former case (which is usually what you want) they are satisfied from whatever file or device is connected to standard input of the Python interpreter. <P> When a script file is used, it is sometimes useful to be able to run the script and enter interactive mode afterwards. This can be done by passing <b class="programopt">-i</b> before the script. (This does not work if the script is read from standard input, for the same reason as explained in the previous paragraph.) <P> <H2><A NAME="SECTION004110000000000000000"><!--x--></A><A NAME="argPassing"><!--z--></A> <BR> 2.1.1 Argument Passing </H2> <P> When known to the interpreter, the script name and additional arguments thereafter are passed to the script in the variable <code>sys.argv</code>, which is a list of strings. Its length is at least one; when no script and no arguments are given, <code>sys.argv[0]</code> is an empty string. When the script name is given as <code>'-'</code> (meaning standard input), <code>sys.argv[0]</code> is set to <code>'-'</code>. When <b class="programopt">-c</b> <var>command</var> is used, <code>sys.argv[0]</code> is set to <code>'-c'</code>. Options found after <b class="programopt">-c</b> <var>command</var> are not consumed by the Python interpreter's option processing but left in <code>sys.argv</code> for the command to handle. <P> <H2><A NAME="SECTION004120000000000000000"><!--x--></A><A NAME="interactive"><!--z--></A> <BR> 2.1.2 Interactive Mode </H2> <P> When commands are read from a tty, the interpreter is said to be in <i>interactive mode</i>. In this mode it prompts for the next command with the <i>primary prompt</i>, usually three greater-than signs ("<tt class="samp">><code>></code>> </tt>"); for continuation lines it prompts with the <i>secondary prompt</i>, by default three dots ("<tt class="samp">... </tt>"). The interpreter prints a welcome message stating its version number and a copyright notice before printing the first prompt: <P> <div class="verbatim"><pre> python Python 1.5.2b2 (#1, Feb 28 1999, 00:02:06) [GCC 2.8.1] on sunos5 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> </pre></div> <P> Continuation lines are needed when entering a multi-line construct. As an example, take a look at this <tt class="keyword">if</tt> statement: <P> <div class="verbatim"><pre> >>> the_world_is_flat = 1 >>> if the_world_is_flat: ... print "Be careful not to fall off!" ... Be careful not to fall off! </pre></div> <P> <H1><A NAME="SECTION004200000000000000000"><!--x--></A><A NAME="interp"><!--z--></A> <BR> 2.2 The Interpreter and Its Environment </H1> <P> <H2><A NAME="SECTION004210000000000000000"><!--x--></A><A NAME="error"><!--z--></A> <BR> 2.2.1 Error Handling </H2> <P> When an error occurs, the interpreter prints an error message and a stack trace. In interactive mode, it then returns to the primary prompt; when input came from a file, it exits with a nonzero exit status after printing the stack trace. (Exceptions handled by an <tt class="keyword">except</tt> clause in a <tt class="keyword">try</tt> statement are not errors in this context.) Some errors are unconditionally fatal and cause an exit with a nonzero exit; this applies to internal inconsistencies and some cases of running out of memory. All error messages are written to the standard error stream; normal output from the executed commands is written to standard output. <P> Typing the interrupt character (usually Control-C or DEL) to the primary or secondary prompt cancels the input and returns to the primary prompt.<A NAME="tex2html1" HREF="#foot111"><SUP>2.1</SUP></A>Typing an interrupt while a command is executing raises the <tt class="exception">KeyboardInterrupt</tt> exception, which may be handled by a <tt class="keyword">try</tt> statement. <P> <H2><A NAME="SECTION004220000000000000000"><!--x--></A><A NAME="scripts"><!--z--></A> <BR> 2.2.2 Executable Python Scripts </H2> <P> On BSD'ish <font style="font-variant: small-caps;">Unix</font> systems, Python scripts can be made directly executable, like shell scripts, by putting the line <P> <div class="verbatim"><pre> #! /usr/bin/env python </pre></div> <P> (assuming that the interpreter is on the user's <a class="envvar" id='l2h-1'>PATH</a>) at the beginning of the script and giving the file an executable mode. The "<tt class="samp">#!</tt>" must be the first two characters of the file. On some platforms, this first line must end with a <font style="font-variant: small-caps;">Unix</font>-style line ending ("<tt class="character">\n</tt>"), not a Mac OS ("<tt class="character">\r</tt>") or Windows ("<tt class="character">\r\n</tt>") line ending. Note that the hash, or pound, character, "<tt class="character">#</tt>", is used to start a comment in Python. <P> The script can be given a executable mode, or permission, using the <b class="program">chmod</b> command: <P> <div class="verbatim"><pre> $ chmod +x myscript.py </pre></div> <P> <H2><A NAME="SECTION004230000000000000000"> 2.2.3 Source Code Encoding</A> </H2> <P> It is possible to use encodings different than ASCII in Python source files. The best way to do it is to put one more special comment line right after the <code>#!</code> line to define the source file encoding: <P> <div class="verbatim"><pre> # -*- coding: iso-8859-1 -*- </pre></div> <P> With that declaration, all characters in the source file will be treated as <code>iso-8859-1</code>, and it will be possible to directly write Unicode string literals in the selected encoding. The list of possible encodings can be found in the <em class="citetitle"><a href="../lib/lib.html" title="Python Library Reference" >Python Library Reference</a></em>, in the section on <a class="ulink" href="../lib/module-codecs.html" ><tt class="module">codecs</tt></a>. <P> If your editor supports saving files as <code>UTF-8</code> with a UTF-8 <i>byte order mark</i> (aka BOM), you can use that instead of an encoding declaration. IDLE supports this capability if <code>Options/General/Default Source Encoding/UTF-8</code> is set. Notice that this signature is not understood in older Python releases (2.2 and earlier), and also not understood by the operating system for <code>#!</code> files. <P> By using UTF-8 (either through the signature or an encoding declaration), characters of most languages in the world can be used simultaneously in string literals and comments. Using non-ASCII characters in identifiers is not supported. To display all these characters properly, your editor must recognize that the file is UTF-8, and it must use a font that supports all the characters in the file. <P> <H2><A NAME="SECTION004240000000000000000"><!--x--></A><A NAME="startup"><!--z--></A> <BR> 2.2.4 The Interactive Startup File </H2> <P> When you use Python interactively, it is frequently handy to have some standard commands executed every time the interpreter is started. You can do this by setting an environment variable named <a class="envvar" id='l2h-2'>PYTHONSTARTUP</a> to the name of a file containing your start-up commands. This is similar to the <span class="file">.profile</span> feature of the <font style="font-variant: small-caps;">Unix</font> shells. <P> This file is only read in interactive sessions, not when Python reads commands from a script, and not when <span class="file">/dev/tty</span> is given as the explicit source of commands (which otherwise behaves like an interactive session). It is executed in the same namespace where interactive commands are executed, so that objects that it defines or imports can be used without qualification in the interactive session. You can also change the prompts <code>sys.ps1</code> and <code>sys.ps2</code> in this file. <P> If you want to read an additional start-up file from the current directory, you can program this in the global start-up file using code like "<tt class="samp">if os.path.isfile('.pythonrc.py'): execfile('.pythonrc.py')</tt>". If you want to use the startup file in a script, you must do this explicitly in the script: <P> <div class="verbatim"><pre> import os filename = os.environ.get('PYTHONSTARTUP') if filename and os.path.isfile(filename): execfile(filename) </pre></div> <P> <BR><HR><H4>Footnotes</H4> <DL> <DT><A NAME="foot111">... prompt.</A><A HREF="node4.html#tex2html1"><SUP>2.1</SUP></A></DT> <DD> A problem with the GNU Readline package may prevent this. </DD> </DL> <DIV CLASS="navigation"> <div class='online-navigation'><hr /> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td class='online-navigation'><a rel="prev" title="1. Whetting Your Appetite" rel="prev" title="1. Whetting Your Appetite" href="node3.html"><img src='../icons/previous.png' border='0' height='32' alt='Previous Page' width='32' /></A></td> <td class='online-navigation'><a rel="parent" title="Python Tutorial" rel="parent" title="Python Tutorial" href="tut.html"><img src='../icons/up.png' border='0' height='32' alt='Up One Level' width='32' /></A></td> <td class='online-navigation'><a rel="next" title="3. An Informal Introduction" rel="next" title="3. An Informal Introduction" href="node5.html"><img src='../icons/next.png' border='0' height='32' alt='Next Page' width='32' /></A></td> <td align="center" width="100%">Python Tutorial</td> <td class='online-navigation'><a rel="contents" title="Table of Contents" rel="contents" title="Table of Contents" href="node2.html"><img src='../icons/contents.png' border='0' height='32' alt='Contents' width='32' /></A></td> <td class='online-navigation'><img src='../icons/blank.png' border='0' height='32' alt='' width='32' /></td> <td class='online-navigation'><a rel="index" title="Index" rel="index" title="Index" href="node18.html"><img src='../icons/index.png' border='0' height='32' alt='Index' width='32' /></A></td> </tr></table> <div class='online-navigation'> <b class="navlabel">Previous:</b> <a class="sectref" rel="prev" href="node3.html">1. Whetting Your Appetite</A> <b class="navlabel">Up:</b> <a class="sectref" rel="parent" href="tut.html">Python Tutorial</A> <b class="navlabel">Next:</b> <a class="sectref" rel="next" href="node5.html">3. An Informal Introduction</A> </div> </div> <hr /> <span class="release-info">Release 2.3.4, documentation updated on May 20, 2004.</span> </DIV> <!--End of Navigation Panel--> <ADDRESS> See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. </ADDRESS> </BODY> </HTML>