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