home *** CD-ROM | disk | FTP | other *** search
- <TITLE>sys -- Python library reference</TITLE>
- Next: <A HREF="../t/types" TYPE="Next">types</A>
- Prev: <A HREF="../p/python_services" TYPE="Prev">Python Services</A>
- Up: <A HREF="../p/python_services" TYPE="Up">Python Services</A>
- Top: <A HREF="../t/top" TYPE="Top">Top</A>
- <H1>3.1. Built-in Module <CODE>sys</CODE></H1>
- This module provides access to some variables used or maintained by the
- interpreter and to functions that interact strongly with the interpreter.
- It is always available.
- <P>
- <DL><DT><B>argv</B> -- data of module sys<DD>
- The list of command line arguments passed to a Python script.
- <CODE>sys.argv[0]</CODE> is the script name (it is operating system
- dependent whether this is a full pathname or not).
- If the command was executed using the `<SAMP>-c</SAMP>' command line option
- to the interpreter, <CODE>sys.argv[0]</CODE> is set to the string
- <CODE>"-c"</CODE>.
- If no script name was passed to the Python interpreter,
- <CODE>sys.argv</CODE> has zero length.
- </DL>
- <DL><DT><B>builtin_module_names</B> -- data of module sys<DD>
- A list of strings giving the names of all modules that are compiled
- into this Python interpreter. (This information is not available in
- any other way --- <CODE>sys.modules.keys()</CODE> only lists the imported
- modules.)
- </DL>
- <DL><DT><B>exc_type</B> -- data of module sys<DD>
- </DL>
- <DL><DT><B>exc_value</B> -- data of module sys<DD>
- </DL>
- <DL><DT><B>exc_traceback</B> -- data of module sys<DD>
- These three variables are not always defined; they are set when an
- exception handler (an <CODE>except</CODE> clause of a <CODE>try</CODE> statement) is
- invoked. Their meaning is: <CODE>exc_type</CODE> gets the exception type of
- the exception being handled; <CODE>exc_value</CODE> gets the exception
- parameter (its <DFN>associated value</DFN> or the second argument to
- <CODE>raise</CODE>); <CODE>exc_traceback</CODE> gets a traceback object (see the
- Reference Manual) which
- encapsulates the call stack at the point where the exception
- originally occurred.
- </DL>
- <DL><DT><B>exit</B> (<VAR>n</VAR>) -- function of module sys<DD>
- Exit from Python with numeric exit status <VAR>n</VAR>. This is
- implemented by raising the <CODE>SystemExit</CODE> exception, so cleanup
- actions specified by <CODE>finally</CODE> clauses of <CODE>try</CODE> statements
- are honored, and it is possible to catch the exit attempt at an outer
- level.
- </DL>
- <DL><DT><B>exitfunc</B> -- data of module sys<DD>
- This value is not actually defined by the module, but can be set by
- the user (or by a program) to specify a clean-up action at program
- exit. When set, it should be a parameterless function. This function
- will be called when the interpreter exits in any way (except when a
- fatal error occurs: in that case the interpreter's internal state
- cannot be trusted).
- </DL>
- <DL><DT><B>last_type</B> -- data of module sys<DD>
- </DL>
- <DL><DT><B>last_value</B> -- data of module sys<DD>
- </DL>
- <DL><DT><B>last_traceback</B> -- data of module sys<DD>
- These three variables are not always defined; they are set when an
- exception is not handled and the interpreter prints an error message
- and a stack traceback. Their intended use is to allow an interactive
- user to import a debugger module and engage in post-mortem debugging
- without having to re-execute the command that caused the error (which
- may be hard to reproduce). The meaning of the variables is the same
- as that of <CODE>exc_type</CODE>, <CODE>exc_value</CODE> and <CODE>exc_tracaback</CODE>,
- respectively.
- </DL>
- <DL><DT><B>modules</B> -- data of module sys<DD>
- Gives the list of modules that have already been loaded.
- This can be manipulated to force reloading of modules and other tricks.
- </DL>
- <DL><DT><B>path</B> -- data of module sys<DD>
- A list of strings that specifies the search path for modules.
- Initialized from the environment variable <CODE>PYTHONPATH</CODE>, or an
- installation-dependent default.
- </DL>
- <DL><DT><B>platform</B> -- data of module sys<DD>
- This string contains a platform identifier. This can be used to
- append platform-specific components to <CODE>sys.path</CODE>, for instance.
- </DL>
- <DL><DT><B>ps1</B> -- data of module sys<DD>
- </DL>
- <DL><DT><B>ps2</B> -- data of module sys<DD>
- Strings specifying the primary and secondary prompt of the
- interpreter. These are only defined if the interpreter is in
- interactive mode. Their initial values in this case are
- <CODE>'>>> '</CODE> and <CODE>'... '</CODE>.
- </DL>
- <DL><DT><B>setcheckinterval</B> (<VAR>interval</VAR>) -- function of module sys<DD>
- Set the interpreter's ``check interval''. This integer value
- determines how often the interpreter checks for periodic things such
- as thread switches and signal handlers. The default is 10, meaning
- the check is performed every 10 Python virtual instructions. Setting
- it to a larger value may increase performance for programs using
- threads. Setting it to a value checks every virtual instruction,
- maximizing responsiveness as well as overhead.
- </DL>
- <DL><DT><B>settrace</B> (<VAR>tracefunc</VAR>) -- function of module sys<DD>
- Set the system's trace function, which allows you to implement a
- Python source code debugger in Python. See section ``How It Works''
- in the chapter on the Python Debugger.
- </DL>
- <DL><DT><B>setprofile</B> (<VAR>profilefunc</VAR>) -- function of module sys<DD>
- Set the system's profile function, which allows you to implement a
- Python source code profiler in Python. See the chapter on the
- Python Profiler. The system's profile function
- is called similarly to the system's trace function (see
- <CODE>sys.settrace</CODE>), but it isn't called for each executed line of
- code (only on call and return and when an exception occurs). Also,
- its return value is not used, so it can just return <CODE>None</CODE>.
- </DL>
- <DL><DT><B>stdin</B> -- data of module sys<DD>
- </DL>
- <DL><DT><B>stdout</B> -- data of module sys<DD>
- </DL>
- <DL><DT><B>stderr</B> -- data of module sys<DD>
- File objects corresponding to the interpreter's standard input,
- output and error streams. <CODE>sys.stdin</CODE> is used for all
- interpreter input except for scripts but including calls to
- <CODE>input()</CODE> and <CODE>raw_input()</CODE>. <CODE>sys.stdout</CODE> is used
- for the output of <CODE>print</CODE> and expression statements and for the
- prompts of <CODE>input()</CODE> and <CODE>raw_input()</CODE>. The interpreter's
- own prompts and (almost all of) its error messages go to
- <CODE>sys.stderr</CODE>. <CODE>sys.stdout</CODE> and <CODE>sys.stderr</CODE> needn't
- be built-in file objects: any object is acceptable as long as it has
- a <CODE>write</CODE> method that takes a string argument. (Changing these
- objects doesn't affect the standard I/O streams of processes
- executed by <CODE>popen()</CODE>, <CODE>system()</CODE> or the <CODE>exec*()</CODE>
- family of functions in the <CODE>os</CODE> module.)
- </DL>
- <DL><DT><B>tracebacklimit</B> -- data of module sys<DD>
- When this variable is set to an integer value, it determines the
- maximum number of levels of traceback information printed when an
- unhandled exception occurs. The default is 1000. When set to 0 or
- less, all traceback information is suppressed and only the exception
- type and value are printed.
- </DL>
-