1 \section{\module{sys
} ---
2 System-specific parameters and functions
}
4 \declaremodule{builtin
}{sys
}
5 \modulesynopsis{Access system-specific parameters and functions.
}
7 This module provides access to some variables used or maintained by the
8 interpreter and to functions that interact strongly with the interpreter.
9 It is always available.
12 \begin{datadesc
}{argv
}
13 The list of command line arguments passed to a Python script.
14 \code{argv
[0]} is the script name (it is operating system dependent
15 whether this is a full pathname or not). If the command was
16 executed using the
\programopt{-c
} command line option to the
17 interpreter,
\code{argv
[0]} is set to the string
\code{'-c'
}. If no
18 script name was passed to the Python interpreter,
\code{argv
} has
22 \begin{datadesc
}{byteorder
}
23 An indicator of the native byte order. This will have the value
24 \code{'big'
} on big-endian (most-signigicant byte first) platforms,
25 and
\code{'little'
} on little-endian (least-significant byte first)
30 \begin{datadesc
}{builtin_module_names
}
31 A tuple of strings giving the names of all modules that are compiled
32 into this Python interpreter. (This information is not available in
33 any other way ---
\code{modules.keys()
} only lists the imported
37 \begin{datadesc
}{copyright
}
38 A string containing the copyright pertaining to the Python
42 \begin{datadesc
}{dllhandle
}
43 Integer specifying the handle of the Python DLL.
44 Availability: Windows.
47 \begin{funcdesc
}{displayhook
}{\var{value
}}
48 If
\var{value
} is not
\code{None
}, this function prints it to
49 \code{sys.stdout
}, and saves it in
\code{__builtin__._
}.
51 \code{sys.displayhook
} is called on the result of evaluating an
52 expression entered in an interactive Python session. The display of
53 these values can be customized by assigning another one-argument
54 function to
\code{sys.displayhook
}.
57 \begin{funcdesc
}{excepthook
}{\var{type
},
\var{value
},
\var{traceback
}}
58 This function prints out a given traceback and exception to
61 When an exception is raised and uncaught, the interpreter calls
62 \code{sys.excepthook
} with three arguments, the exception class,
63 exception instance, and a traceback object. In an interactive
64 session this happens just before control is returned to the prompt;
65 in a Python program this happens just before the program exits. The
66 handling of such top-level exceptions can be customized by assigning
67 another three-argument function to
\code{sys.excepthook
}.
70 \begin{datadesc
}{__displayhook__
}
71 \dataline{__excepthook__
}
72 These objects contain the original values of
\code{displayhook
} and
73 \code{excepthook
} at the start of the program. They are saved so
74 that
\code{displayhook
} and
\code{excepthook
} can be restored in
75 case they happen to get replaced with broken objects.
78 \begin{funcdesc
}{exc_info
}{}
79 This function returns a tuple of three values that give information
80 about the exception that is currently being handled. The
81 information returned is specific both to the current thread and to
82 the current stack frame. If the current stack frame is not handling
83 an exception, the information is taken from the calling stack frame,
84 or its caller, and so on until a stack frame is found that is
85 handling an exception. Here, ``handling an exception'' is defined
86 as ``executing or having executed an except clause.'' For any stack
87 frame, only information about the most recently handled exception is
90 If no exception is being handled anywhere on the stack, a tuple
91 containing three
\code{None
} values is returned. Otherwise, the
92 values returned are
\code{(
\var{type
},
\var{value
},
93 \var{traceback
})
}. Their meaning is:
\var{type
} gets the exception
94 type of the exception being handled (a string or class object);
95 \var{value
} gets the exception parameter (its
\dfn{associated value
}
96 or the second argument to
\keyword{raise
}, which is always a class
97 instance if the exception type is a class object);
\var{traceback
}
98 gets a traceback object (see the Reference Manual) which
99 encapsulates the call stack at the point where the exception
100 originally occurred.
\obindex{traceback
}
102 \warning{Assigning the
\var{traceback
} return value to a
103 local variable in a function that is handling an exception will
104 cause a circular reference. This will prevent anything referenced
105 by a local variable in the same function or by the traceback from
106 being garbage collected. Since most functions don't need access to
107 the traceback, the best solution is to use something like
108 \code{exctype, value = sys.exc_info()
[:
2]} to extract only the
109 exception type and value. If you do need the traceback, make sure
110 to delete it after use (best done with a
\keyword{try
}
111 ...
\keyword{finally
} statement) or to call
\function{exc_info()
} in
112 a function that does not itself handle an exception.
} \note{Beginning
113 with Python
2.2, such cycles are automatically reclaimed when garbage
114 collection is enabled and they become unreachable, but it remains more
115 efficient to avoid creating cycles.
}
118 \begin{datadesc
}{exc_type
}
120 \dataline{exc_traceback
}
122 {Use
\function{exc_info()
} instead.
}
123 Since they are global variables, they are not specific to the
124 current thread, so their use is not safe in a multi-threaded
125 program. When no exception is being handled,
\code{exc_type
} is set
126 to
\code{None
} and the other two are undefined.
129 \begin{datadesc
}{exec_prefix
}
130 A string giving the site-specific directory prefix where the
131 platform-dependent Python files are installed; by default, this is
132 also
\code{'/usr/local'
}. This can be set at build time with the
133 \longprogramopt{exec-prefix
} argument to the
\program{configure
}
134 script. Specifically, all configuration files (e.g. the
135 \file{pyconfig.h
} header file) are installed in the directory
136 \code{exec_prefix + '/lib/python
\var{version
}/config'
}, and shared
137 library modules are installed in
\code{exec_prefix +
138 '/lib/python
\var{version
}/lib-dynload'
}, where
\var{version
} is
139 equal to
\code{version
[:
3]}.
142 \begin{datadesc
}{executable
}
143 A string giving the name of the executable binary for the Python
144 interpreter, on systems where this makes sense.
147 \begin{funcdesc
}{exit
}{\optional{arg
}}
148 Exit from Python. This is implemented by raising the
149 \exception{SystemExit
} exception, so cleanup actions specified by
150 finally clauses of
\keyword{try
} statements are honored, and it is
151 possible to intercept the exit attempt at an outer level. The
152 optional argument
\var{arg
} can be an integer giving the exit status
153 (defaulting to zero), or another type of object. If it is an
154 integer, zero is considered ``successful termination'' and any
155 nonzero value is considered ``abnormal termination'' by shells and
156 the like. Most systems require it to be in the range
0-
127, and
157 produce undefined results otherwise. Some systems have a convention
158 for assigning specific meanings to specific exit codes, but these
159 are generally underdeveloped;
\UNIX{} programs generally use
2 for
160 command line syntax errors and
1 for all other kind of errors. If
161 another type of object is passed,
\code{None
} is equivalent to
162 passing zero, and any other object is printed to
\code{sys.stderr
}
163 and results in an exit code of
1. In particular,
164 \code{sys.exit("some error message")
} is a quick way to exit a
165 program when an error occurs.
168 \begin{datadesc
}{exitfunc
}
169 This value is not actually defined by the module, but can be set by
170 the user (or by a program) to specify a clean-up action at program
171 exit. When set, it should be a parameterless function. This
172 function will be called when the interpreter exits. Only one
173 function may be installed in this way; to allow multiple functions
174 which will be called at termination, use the
\refmodule{atexit
}
175 module.
\note{The exit function is not called when the program is
176 killed by a signal, when a Python fatal internal error is detected,
177 or when
\code{os._exit()
} is called.
}
180 \begin{funcdesc
}{getdefaultencoding
}{}
181 Return the name of the current default string encoding used by the
182 Unicode implementation.
186 \begin{funcdesc
}{getdlopenflags
}{}
187 Return the current value of the flags that are used for
188 \cfunction{dlopen()
} calls. The flag constants are defined in the
189 \refmodule{dl
} and
\module{DLFCN
} modules.
194 \begin{funcdesc
}{getrefcount
}{object
}
195 Return the reference count of the
\var{object
}. The count returned
196 is generally one higher than you might expect, because it includes
197 the (temporary) reference as an argument to
198 \function{getrefcount()
}.
201 \begin{funcdesc
}{getrecursionlimit
}{}
202 Return the current value of the recursion limit, the maximum depth
203 of the Python interpreter stack. This limit prevents infinite
204 recursion from causing an overflow of the C stack and crashing
205 Python. It can be set by
\function{setrecursionlimit()
}.
208 \begin{funcdesc
}{_getframe
}{\optional{depth
}}
209 Return a frame object from the call stack. If optional integer
210 \var{depth
} is given, return the frame object that many calls below
211 the top of the stack. If that is deeper than the call stack,
212 \exception{ValueError
} is raised. The default for
\var{depth
} is
213 zero, returning the frame at the top of the call stack.
215 This function should be used for internal and specialized purposes
219 \begin{datadesc
}{hexversion
}
220 The version number encoded as a single integer. This is guaranteed
221 to increase with each version, including proper support for
222 non-production releases. For example, to test that the Python
223 interpreter is at least version
1.5.2, use:
226 if sys.hexversion >=
0x010502F0:
227 # use some advanced feature
230 # use an alternative implementation or warn the user
234 This is called
\samp{hexversion
} since it only really looks
235 meaningful when viewed as the result of passing it to the built-in
236 \function{hex()
} function. The
\code{version_info
} value may be
237 used for a more human-friendly encoding of the same information.
241 \begin{datadesc
}{last_type
}
242 \dataline{last_value
}
243 \dataline{last_traceback
}
244 These three variables are not always defined; they are set when an
245 exception is not handled and the interpreter prints an error message
246 and a stack traceback. Their intended use is to allow an
247 interactive user to import a debugger module and engage in
248 post-mortem debugging without having to re-execute the command that
249 caused the error. (Typical use is
\samp{import pdb; pdb.pm()
} to
250 enter the post-mortem debugger; see chapter
\ref{debugger
}, ``The
251 Python Debugger,'' for more information.)
253 The meaning of the variables is the same as that of the return
254 values from
\function{exc_info()
} above. (Since there is only one
255 interactive thread, thread-safety is not a concern for these
256 variables, unlike for
\code{exc_type
} etc.)
259 \begin{datadesc
}{maxint
}
260 The largest positive integer supported by Python's regular integer
261 type. This is at least
2**
31-
1. The largest negative integer is
262 \code{-maxint-
1} --- the asymmetry results from the use of
2's
263 complement binary arithmetic.
266 \begin{datadesc
}{maxunicode
}
267 An integer giving the largest supported code point for a Unicode
268 character. The value of this depends on the configuration option
269 that specifies whether Unicode characters are stored as UCS-
2 or
273 \begin{datadesc
}{modules
}
274 This is a dictionary that maps module names to modules which have
275 already been loaded. This can be manipulated to force reloading of
276 modules and other tricks. Note that removing a module from this
277 dictionary is
\emph{not
} the same as calling
278 \function{reload()
}\bifuncindex{reload
} on the corresponding module
282 \begin{datadesc
}{path
}
283 \indexiii{module
}{search
}{path
}
284 A list of strings that specifies the search path for modules.
285 Initialized from the environment variable
\envvar{PYTHONPATH
}, or an
286 installation-dependent default.
288 The first item of this list,
\code{path
[0]}, is the directory
289 containing the script that was used to invoke the Python
290 interpreter. If the script directory is not available (e.g. if the
291 interpreter is invoked interactively or if the script is read from
292 standard input),
\code{path
[0]} is the empty string, which directs
293 Python to search modules in the current directory first. Notice
294 that the script directory is inserted
\emph{before
} the entries
295 inserted as a result of
\envvar{PYTHONPATH
}.
298 \begin{datadesc
}{platform
}
299 This string contains a platform identifier, e.g.
\code{'sunos5'
} or
300 \code{'linux1'
}. This can be used to append platform-specific
301 components to
\code{path
}, for instance.
304 \begin{datadesc
}{prefix
}
305 A string giving the site-specific directory prefix where the
306 platform independent Python files are installed; by default, this is
307 the string
\code{'/usr/local'
}. This can be set at build time with
308 the
\longprogramopt{prefix
} argument to the
\program{configure
}
309 script. The main collection of Python library modules is installed
310 in the directory
\code{prefix + '/lib/python
\var{version
}'
} while
311 the platform independent header files (all except
\file{pyconfig.h
})
312 are stored in
\code{prefix + '/include/python
\var{version
}'
}, where
313 \var{version
} is equal to
\code{version
[:
3]}.
316 \begin{datadesc
}{ps1
}
318 \index{interpreter prompts
}
319 \index{prompts, interpreter
}
320 Strings specifying the primary and secondary prompt of the
321 interpreter. These are only defined if the interpreter is in
322 interactive mode. Their initial values in this case are
323 \code{'>
\code{>
}> '
} and
\code{'... '
}. If a non-string object is
324 assigned to either variable, its
\function{str()
} is re-evaluated
325 each time the interpreter prepares to read a new interactive
326 command; this can be used to implement a dynamic prompt.
329 \begin{funcdesc
}{setcheckinterval
}{interval
}
330 Set the interpreter's ``check interval''. This integer value
331 determines how often the interpreter checks for periodic things such
332 as thread switches and signal handlers. The default is
\code{10},
333 meaning the check is performed every
10 Python virtual instructions.
334 Setting it to a larger value may increase performance for programs
335 using threads. Setting it to a value
\code{<=
} 0 checks every
336 virtual instruction, maximizing responsiveness as well as overhead.
339 \begin{funcdesc
}{setdefaultencoding
}{name
}
340 Set the current default string encoding used by the Unicode
341 implementation. If
\var{name
} does not match any available
342 encoding,
\exception{LookupError
} is raised. This function is only
343 intended to be used by the
\refmodule{site
} module implementation
344 and, where needed, by
\module{sitecustomize
}. Once used by the
345 \refmodule{site
} module, it is removed from the
\module{sys
}
347 % Note that \refmodule{site} is not imported if
348 % the \programopt{-S} option is passed to the interpreter, in which
349 % case this function will remain available.
353 \begin{funcdesc
}{setdlopenflags
}{n
}
354 Set the flags used by the interpreter for
\cfunction{dlopen()
}
355 calls, such as when the interpreter loads extension modules. Among
356 other things, this will enable a lazy resolving of symbols when
357 importing a module, if called as
\code{sys.setdlopenflags(
0)
}. To
358 share symbols across extension modules, call as
359 \code{sys.setdlopenflags(dl.RTLD_NOW | dl.RTLD_GLOBAL)
}. Symbolic
360 names for the flag modules can be either found in the
\refmodule{dl
}
361 module, or in the
\module{DLFCN
} module. If
\module{DLFCN
} is not
362 available, it can be generated from
\file{/usr/include/dlfcn.h
}
363 using the
\program{h2py
} script.
368 \begin{funcdesc
}{setprofile
}{profilefunc
}
369 Set the system's profile function,
\index{profile function
} which
370 allows you to implement a Python source code profiler in
371 Python.
\index{profiler
} See chapter
\ref{profile
} for more
372 information on the Python profiler. The system's profile function
373 is called similarly to the system's trace function (see
374 \function{settrace()
}), but it isn't called for each executed line
375 of code (only on call and return, but the return event is reported
376 even when an exception has been set). The function is
377 thread-specific, but there is no way for the profiler to know about
378 context switches between threads, so it does not make sense to use
379 this in the presence of multiple threads.
380 Also, its return value is not used, so it can simply return
384 \begin{funcdesc
}{setrecursionlimit
}{limit
}
385 Set the maximum depth of the Python interpreter stack to
386 \var{limit
}. This limit prevents infinite recursion from causing an
387 overflow of the C stack and crashing Python.
389 The highest possible limit is platform-dependent. A user may need
390 to set the limit higher when she has a program that requires deep
391 recursion and a platform that supports a higher limit. This should
392 be done with care, because a too-high limit can lead to a crash.
395 \begin{funcdesc
}{settrace
}{tracefunc
}
396 Set the system's trace function,
\index{trace function
} which allows
397 you to implement a Python source code debugger in Python. See
398 section
\ref{debugger-hooks
}, ``How It Works,'' in the chapter on
399 the Python debugger.
\index{debugger
} The function is
400 thread-specific; for a debugger to support multiple threads, it must
401 be registered using
\function{settrace()
} for each thread being
405 \begin{datadesc
}{stdin
}
408 File objects corresponding to the interpreter's standard input,
409 output and error streams.
\code{stdin
} is used for all interpreter
410 input except for scripts but including calls to
411 \function{input()
}\bifuncindex{input
} and
412 \function{raw_input()
}\bifuncindex{raw_input
}.
\code{stdout
} is
413 used for the output of
\keyword{print
} and expression statements and
414 for the prompts of
\function{input()
} and
\function{raw_input()
}.
415 The interpreter's own prompts and (almost all of) its error messages
416 go to
\code{stderr
}.
\code{stdout
} and
\code{stderr
} needn't be
417 built-in file objects: any object is acceptable as long as it has a
418 \method{write()
} method that takes a string argument. (Changing
419 these objects doesn't affect the standard I/O streams of processes
420 executed by
\function{os.popen()
},
\function{os.system()
} or the
421 \function{exec*()
} family of functions in the
\refmodule{os
}
425 \begin{datadesc
}{__stdin__
}
426 \dataline{__stdout__
}
427 \dataline{__stderr__
}
428 These objects contain the original values of
\code{stdin
},
429 \code{stderr
} and
\code{stdout
} at the start of the program. They
430 are used during finalization, and could be useful to restore the
431 actual files to known working file objects in case they have been
432 overwritten with a broken object.
435 \begin{datadesc
}{tracebacklimit
}
436 When this variable is set to an integer value, it determines the
437 maximum number of levels of traceback information printed when an
438 unhandled exception occurs. The default is
\code{1000}. When set
439 to
\code{0} or less, all traceback information is suppressed and
440 only the exception type and value are printed.
443 \begin{datadesc
}{version
}
444 A string containing the version number of the Python interpreter
445 plus additional information on the build number and compiler used.
446 It has a value of the form
\code{'
\var{version
}
447 (\#
\var{build_number
},
\var{build_date
},
\var{build_time
})
448 [\var{compiler
}]'
}. The first three characters are used to identify
449 the version in the installation directories (where appropriate on
450 each platform). An example:
455 '
1.5.2 (
#0 Apr
13 1999,
10:
51:
12)
[MSC
32 bit (Intel)
]'
459 \begin{datadesc
}{version_info
}
460 A tuple containing the five components of the version number:
461 \var{major
},
\var{minor
},
\var{micro
},
\var{releaselevel
}, and
462 \var{serial
}. All values except
\var{releaselevel
} are integers;
463 the release level is
\code{'alpha'
},
\code{'beta'
},
464 \code{'candidate'
}, or
\code{'final'
}. The
\code{version_info
}
465 value corresponding to the Python version
2.0 is
\code{(
2,
0,
0,
470 \begin{datadesc
}{warnoptions
}
471 This is an implementation detail of the warnings framework; do not
472 modify this value. Refer to the
\refmodule{warnings
} module for
473 more information on the warnings framework.
476 \begin{datadesc
}{winver
}
477 The version number used to form registry keys on Windows platforms.
478 This is stored as string resource
1000 in the Python DLL. The value
479 is normally the first three characters of
\constant{version
}. It is
480 provided in the
\module{sys
} module for informational purposes;
481 modifying this value has no effect on the registry keys used by
483 Availability: Windows.
489 {This describes how to use .pth files to extend
\code{sys.path
}.
}