Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1 | \section{Built-in Module \sectcode{sys}} |
| 2 | |
| 3 | \bimodindex{sys} |
| 4 | This module provides access to some variables used or maintained by the |
| 5 | interpreter and to functions that interact strongly with the interpreter. |
| 6 | It is always available. |
| 7 | |
| 8 | \renewcommand{\indexsubitem}{(in module sys)} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 10 | \begin{datadesc}{argv} |
| 11 | The list of command line arguments passed to a Python script. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 12 | \code{sys.argv[0]} is the script name (it is operating system |
| 13 | dependent whether this is a full pathname or not). |
| 14 | If the command was executed using the \samp{-c} command line option |
| 15 | to the interpreter, \code{sys.argv[0]} is set to the string |
| 16 | \code{"-c"}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 17 | If no script name was passed to the Python interpreter, |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 18 | \code{sys.argv} has zero length. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 19 | \end{datadesc} |
| 20 | |
| 21 | \begin{datadesc}{builtin_module_names} |
Guido van Rossum | 0d2971b | 1997-01-06 23:01:02 +0000 | [diff] [blame] | 22 | A tuple of strings giving the names of all modules that are compiled |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 23 | into this Python interpreter. (This information is not available in |
| 24 | any other way --- \code{sys.modules.keys()} only lists the imported |
| 25 | modules.) |
| 26 | \end{datadesc} |
| 27 | |
| 28 | \begin{datadesc}{exc_type} |
| 29 | \dataline{exc_value} |
| 30 | \dataline{exc_traceback} |
| 31 | These three variables are not always defined; they are set when an |
| 32 | exception handler (an \code{except} clause of a \code{try} statement) is |
| 33 | invoked. Their meaning is: \code{exc_type} gets the exception type of |
| 34 | the exception being handled; \code{exc_value} gets the exception |
| 35 | parameter (its \dfn{associated value} or the second argument to |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 36 | \code{raise}); \code{exc_traceback} gets a traceback object (see the |
| 37 | Reference Manual) which |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 38 | encapsulates the call stack at the point where the exception |
| 39 | originally occurred. |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 40 | \obindex{traceback} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 41 | \end{datadesc} |
| 42 | |
Guido van Rossum | 0a3c753 | 1997-06-02 17:32:41 +0000 | [diff] [blame] | 43 | \begin{datadesc}{exec_prefix} |
| 44 | A string giving the site-specific |
| 45 | directory prefix where the platform-dependent Python files are |
| 46 | installed; by default, this is also \code{"/usr/local"}. This can be |
| 47 | set at build time with the \code{--exec-prefix} argument to the |
| 48 | \code{configure} script. Specifically, all configuration files |
| 49 | (e.g. the \code{config.h} header file) are installed in the directory |
| 50 | \code{sys.exec_prefix+"/lib/python\emph{VER}/config"}, and shared library |
| 51 | modules are installed in |
| 52 | \code{sys.exec_prefix+"/lib/python\emph{VER}/sharedmodules"}, |
| 53 | where \emph{VER} is equal to \code{sys.version[:3]}. |
| 54 | \end{datadesc} |
| 55 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 56 | \begin{funcdesc}{exit}{n} |
| 57 | Exit from Python with numeric exit status \var{n}. This is |
| 58 | implemented by raising the \code{SystemExit} exception, so cleanup |
| 59 | actions specified by \code{finally} clauses of \code{try} statements |
| 60 | are honored, and it is possible to catch the exit attempt at an outer |
| 61 | level. |
| 62 | \end{funcdesc} |
| 63 | |
| 64 | \begin{datadesc}{exitfunc} |
| 65 | This value is not actually defined by the module, but can be set by |
| 66 | the user (or by a program) to specify a clean-up action at program |
| 67 | exit. When set, it should be a parameterless function. This function |
Guido van Rossum | 6b686e9 | 1995-07-07 23:00:35 +0000 | [diff] [blame] | 68 | will be called when the interpreter exits in any way (except when a |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 69 | fatal error occurs: in that case the interpreter's internal state |
| 70 | cannot be trusted). |
| 71 | \end{datadesc} |
| 72 | |
| 73 | \begin{datadesc}{last_type} |
| 74 | \dataline{last_value} |
| 75 | \dataline{last_traceback} |
| 76 | These three variables are not always defined; they are set when an |
| 77 | exception is not handled and the interpreter prints an error message |
| 78 | and a stack traceback. Their intended use is to allow an interactive |
| 79 | user to import a debugger module and engage in post-mortem debugging |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 80 | without having to re-execute the command that caused the error (which |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 81 | may be hard to reproduce). The meaning of the variables is the same |
| 82 | as that of \code{exc_type}, \code{exc_value} and \code{exc_tracaback}, |
| 83 | respectively. |
| 84 | \end{datadesc} |
| 85 | |
| 86 | \begin{datadesc}{modules} |
| 87 | Gives the list of modules that have already been loaded. |
| 88 | This can be manipulated to force reloading of modules and other tricks. |
| 89 | \end{datadesc} |
| 90 | |
| 91 | \begin{datadesc}{path} |
| 92 | A list of strings that specifies the search path for modules. |
| 93 | Initialized from the environment variable \code{PYTHONPATH}, or an |
Guido van Rossum | 0a3c753 | 1997-06-02 17:32:41 +0000 | [diff] [blame] | 94 | installation-dependent default. |
| 95 | |
| 96 | The first item of this list, \code{sys.path[0]}, is the |
| 97 | directory containing the script that was used to invoke the Python |
| 98 | interpreter. If the script directory is not available (e.g. if the |
| 99 | interpreter is invoked interactively or if the script is read from |
| 100 | standard input), \code{sys.path[0]} is the empty string, which directs |
| 101 | Python to search modules in the current directory first. Notice that |
| 102 | the script directory is inserted {\em before} the entries inserted as |
| 103 | a result of \code{\$PYTHONPATH}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 104 | \end{datadesc} |
| 105 | |
Guido van Rossum | 6b686e9 | 1995-07-07 23:00:35 +0000 | [diff] [blame] | 106 | \begin{datadesc}{platform} |
Guido van Rossum | 0a3c753 | 1997-06-02 17:32:41 +0000 | [diff] [blame] | 107 | This string contains a platform identifier, e.g. \code{sunos5} or |
| 108 | \code{linux1}. This can be used to append platform-specific |
| 109 | components to \code{sys.path}, for instance. |
| 110 | \end{datadesc} |
| 111 | |
| 112 | \begin{datadesc}{prefix} |
| 113 | A string giving the site-specific directory prefix where the platform |
| 114 | independent Python files are installed; by default, this is the string |
| 115 | \code{"/usr/local"}. This can be set at build time with the |
| 116 | \code{--prefix} argument to the \code{configure} script. The main |
| 117 | collection of Python library modules is installed in the directory |
| 118 | \code{sys.prefix+"/lib/python\emph{VER}"} while the platform |
| 119 | independent header files (all except \code{config.h}) are stored in |
| 120 | \code{sys.prefix+"/include/python\emph{VER}"}, |
| 121 | where \emph{VER} is equal to \code{sys.version[:3]}. |
| 122 | |
Guido van Rossum | 6b686e9 | 1995-07-07 23:00:35 +0000 | [diff] [blame] | 123 | \end{datadesc} |
| 124 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 125 | \begin{datadesc}{ps1} |
| 126 | \dataline{ps2} |
| 127 | Strings specifying the primary and secondary prompt of the |
| 128 | interpreter. These are only defined if the interpreter is in |
| 129 | interactive mode. Their initial values in this case are |
| 130 | \code{'>>> '} and \code{'... '}. |
| 131 | \end{datadesc} |
| 132 | |
Guido van Rossum | 9c51e41 | 1995-01-10 10:50:58 +0000 | [diff] [blame] | 133 | \begin{funcdesc}{setcheckinterval}{interval} |
| 134 | Set the interpreter's ``check interval''. This integer value |
| 135 | determines how often the interpreter checks for periodic things such |
| 136 | as thread switches and signal handlers. The default is 10, meaning |
| 137 | the check is performed every 10 Python virtual instructions. Setting |
| 138 | it to a larger value may increase performance for programs using |
Guido van Rossum | 6c4f003 | 1995-03-07 10:14:09 +0000 | [diff] [blame] | 139 | threads. Setting it to a value $\leq 0$ checks every virtual instruction, |
Guido van Rossum | 9c51e41 | 1995-01-10 10:50:58 +0000 | [diff] [blame] | 140 | maximizing responsiveness as well as overhead. |
Guido van Rossum | 7f49b7a | 1995-01-12 12:38:46 +0000 | [diff] [blame] | 141 | \end{funcdesc} |
Guido van Rossum | 9c51e41 | 1995-01-10 10:50:58 +0000 | [diff] [blame] | 142 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 143 | \begin{funcdesc}{settrace}{tracefunc} |
| 144 | Set the system's trace function, which allows you to implement a |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 145 | Python source code debugger in Python. See section ``How It Works'' |
| 146 | in the chapter on the Python Debugger. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 147 | \end{funcdesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 148 | \index{trace function} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 149 | \index{debugger} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 150 | |
| 151 | \begin{funcdesc}{setprofile}{profilefunc} |
| 152 | Set the system's profile function, which allows you to implement a |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 153 | Python source code profiler in Python. See the chapter on the |
| 154 | Python Profiler. The system's profile function |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 155 | is called similarly to the system's trace function (see |
| 156 | \code{sys.settrace}), but it isn't called for each executed line of |
| 157 | code (only on call and return and when an exception occurs). Also, |
| 158 | its return value is not used, so it can just return \code{None}. |
| 159 | \end{funcdesc} |
| 160 | \index{profile function} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 161 | \index{profiler} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 162 | |
| 163 | \begin{datadesc}{stdin} |
| 164 | \dataline{stdout} |
| 165 | \dataline{stderr} |
| 166 | File objects corresponding to the interpreter's standard input, |
| 167 | output and error streams. \code{sys.stdin} is used for all |
| 168 | interpreter input except for scripts but including calls to |
| 169 | \code{input()} and \code{raw_input()}. \code{sys.stdout} is used |
| 170 | for the output of \code{print} and expression statements and for the |
| 171 | prompts of \code{input()} and \code{raw_input()}. The interpreter's |
| 172 | own prompts and (almost all of) its error messages go to |
| 173 | \code{sys.stderr}. \code{sys.stdout} and \code{sys.stderr} needn't |
| 174 | be built-in file objects: any object is acceptable as long as it has |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 175 | a \code{write} method that takes a string argument. (Changing these |
| 176 | objects doesn't affect the standard I/O streams of processes |
| 177 | executed by \code{popen()}, \code{system()} or the \code{exec*()} |
| 178 | family of functions in the \code{os} module.) |
| 179 | \stmodindex{os} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 180 | \end{datadesc} |
| 181 | |
| 182 | \begin{datadesc}{tracebacklimit} |
| 183 | When this variable is set to an integer value, it determines the |
| 184 | maximum number of levels of traceback information printed when an |
| 185 | unhandled exception occurs. The default is 1000. When set to 0 or |
| 186 | less, all traceback information is suppressed and only the exception |
| 187 | type and value are printed. |
| 188 | \end{datadesc} |
Guido van Rossum | 0a3c753 | 1997-06-02 17:32:41 +0000 | [diff] [blame] | 189 | |
| 190 | \begin{datadesc}{version} |
| 191 | A string containing the version number of the Python interpreter. |
| 192 | \end{datadesc} |