blob: 1e89bd02eed446cad5124e6f02d6f0a030a39e34 [file] [log] [blame]
Tarek Ziadé18755702010-02-02 23:17:47 +00001:mod:`sysconfig` --- Provide access to Python's configuration information
2=========================================================================
3
4.. module:: sysconfig
5 :synopsis: Python's configuration information
6.. moduleauthor:: Tarek Ziade <tarek@ziade.org>
7.. sectionauthor:: Tarek Ziade <tarek@ziade.org>
Tarek Ziadé18755702010-02-02 23:17:47 +00008.. index::
9 single: configuration information
10
Raymond Hettingera1993682011-01-27 01:20:32 +000011**Source code:** :source:`Lib/sysconfig.py`
12
13.. versionadded:: 3.2
14
15--------------
16
Tarek Ziadé18755702010-02-02 23:17:47 +000017The :mod:`sysconfig` module provides access to Python's configuration
Tarek Ziadé667882f2010-02-11 20:47:18 +000018information like the list of installation paths and the configuration variables
19relevant for the current platform.
Tarek Ziadé18755702010-02-02 23:17:47 +000020
21Configuration variables
22-----------------------
23
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000024A Python distribution contains a :file:`Makefile` and a :file:`pyconfig.h`
25header file that are necessary to build both the Python binary itself and
26third-party C extensions compiled using :mod:`distutils`.
Tarek Ziadé18755702010-02-02 23:17:47 +000027
Tarek Ziadé667882f2010-02-11 20:47:18 +000028:mod:`sysconfig` puts all variables found in these files in a dictionary that
29can be accessed using :func:`get_config_vars` or :func:`get_config_var`.
Tarek Ziadé18755702010-02-02 23:17:47 +000030
31Notice that on Windows, it's a much smaller set.
32
33.. function:: get_config_vars(\*args)
34
Tarek Ziadé667882f2010-02-11 20:47:18 +000035 With no arguments, return a dictionary of all configuration variables
36 relevant for the current platform.
Tarek Ziadé18755702010-02-02 23:17:47 +000037
Tarek Ziadé667882f2010-02-11 20:47:18 +000038 With arguments, return a list of values that result from looking up each
39 argument in the configuration variable dictionary.
Tarek Ziadé18755702010-02-02 23:17:47 +000040
Tarek Ziadé667882f2010-02-11 20:47:18 +000041 For each argument, if the value is not found, return ``None``.
42
Tarek Ziadé18755702010-02-02 23:17:47 +000043
44.. function:: get_config_var(name)
45
46 Return the value of a single variable *name*. Equivalent to
Tarek Ziadé667882f2010-02-11 20:47:18 +000047 ``get_config_vars().get(name)``.
Tarek Ziadé18755702010-02-02 23:17:47 +000048
Tarek Ziadé667882f2010-02-11 20:47:18 +000049 If *name* is not found, return ``None``.
Tarek Ziadé18755702010-02-02 23:17:47 +000050
51Example of usage::
52
Tarek Ziadé667882f2010-02-11 20:47:18 +000053 >>> import sysconfig
54 >>> sysconfig.get_config_var('Py_ENABLE_SHARED')
55 0
56 >>> sysconfig.get_config_var('LIBDIR')
57 '/usr/local/lib'
58 >>> sysconfig.get_config_vars('AR', 'CXX')
59 ['ar', 'g++']
Tarek Ziadé18755702010-02-02 23:17:47 +000060
61
62Installation paths
63------------------
64
Tarek Ziadé667882f2010-02-11 20:47:18 +000065Python uses an installation scheme that differs depending on the platform and on
66the installation options. These schemes are stored in :mod:`sysconfig` under
67unique identifiers based on the value returned by :const:`os.name`.
Tarek Ziadé18755702010-02-02 23:17:47 +000068
69Every new component that is installed using :mod:`distutils` or a
Tarek Ziadé667882f2010-02-11 20:47:18 +000070Distutils-based system will follow the same scheme to copy its file in the right
71places.
Tarek Ziadé18755702010-02-02 23:17:47 +000072
73Python currently supports seven schemes:
74
Tarek Ziadé667882f2010-02-11 20:47:18 +000075- *posix_prefix*: scheme for Posix platforms like Linux or Mac OS X. This is
76 the default scheme used when Python or a component is installed.
77- *posix_home*: scheme for Posix platforms used when a *home* option is used
78 upon installation. This scheme is used when a component is installed through
Tarek Ziadé18755702010-02-02 23:17:47 +000079 Distutils with a specific home prefix.
Tarek Ziadé667882f2010-02-11 20:47:18 +000080- *posix_user*: scheme for Posix platforms used when a component is installed
81 through Distutils and the *user* option is used. This scheme defines paths
Tarek Ziadé18755702010-02-02 23:17:47 +000082 located under the user home directory.
Tarek Ziadé667882f2010-02-11 20:47:18 +000083- *nt*: scheme for NT platforms like Windows.
84- *nt_user*: scheme for NT platforms, when the *user* option is used.
85- *os2*: scheme for OS/2 platforms.
86- *os2_home*: scheme for OS/2 patforms, when the *user* option is used.
Tarek Ziadé18755702010-02-02 23:17:47 +000087
88Each scheme is itself composed of a series of paths and each path has a unique
Tarek Ziadé667882f2010-02-11 20:47:18 +000089identifier. Python currently uses eight paths:
Tarek Ziadé18755702010-02-02 23:17:47 +000090
Tarek Ziadé667882f2010-02-11 20:47:18 +000091- *stdlib*: directory containing the standard Python library files that are not
92 platform-specific.
93- *platstdlib*: directory containing the standard Python library files that are
94 platform-specific.
95- *platlib*: directory for site-specific, platform-specific files.
96- *purelib*: directory for site-specific, non-platform-specific files.
97- *include*: directory for non-platform-specific header files.
98- *platinclude*: directory for platform-specific header files.
99- *scripts*: directory for script files.
100- *data*: directory for data files.
Tarek Ziadé18755702010-02-02 23:17:47 +0000101
Tarek Ziadé667882f2010-02-11 20:47:18 +0000102:mod:`sysconfig` provides some functions to determine these paths.
Tarek Ziadé18755702010-02-02 23:17:47 +0000103
104.. function:: get_scheme_names()
105
106 Return a tuple containing all schemes currently supported in
107 :mod:`sysconfig`.
108
Tarek Ziadé667882f2010-02-11 20:47:18 +0000109
Tarek Ziadé18755702010-02-02 23:17:47 +0000110.. function:: get_path_names()
111
112 Return a tuple containing all path names currently supported in
113 :mod:`sysconfig`.
114
115
116.. function:: get_path(name, [scheme, [vars, [expand]]])
117
118 Return an installation path corresponding to the path *name*, from the
119 install scheme named *scheme*.
120
121 *name* has to be a value from the list returned by :func:`get_path_names`.
122
Tarek Ziadé667882f2010-02-11 20:47:18 +0000123 :mod:`sysconfig` stores installation paths corresponding to each path name,
124 for each platform, with variables to be expanded. For instance the *stdlib*
125 path for the *nt* scheme is: ``{base}/Lib``.
Tarek Ziadé18755702010-02-02 23:17:47 +0000126
127 :func:`get_path` will use the variables returned by :func:`get_config_vars`
Tarek Ziadé667882f2010-02-11 20:47:18 +0000128 to expand the path. All variables have default values for each platform so
129 one may call this function and get the default value.
Tarek Ziadé18755702010-02-02 23:17:47 +0000130
131 If *scheme* is provided, it must be a value from the list returned by
Tarek Ziadé667882f2010-02-11 20:47:18 +0000132 :func:`get_path_names`. Otherwise, the default scheme for the current
Tarek Ziadé18755702010-02-02 23:17:47 +0000133 platform is used.
134
Tarek Ziadé667882f2010-02-11 20:47:18 +0000135 If *vars* is provided, it must be a dictionary of variables that will update
136 the dictionary return by :func:`get_config_vars`.
Tarek Ziadé18755702010-02-02 23:17:47 +0000137
Tarek Ziadé667882f2010-02-11 20:47:18 +0000138 If *expand* is set to ``False``, the path will not be expanded using the
139 variables.
Tarek Ziadé18755702010-02-02 23:17:47 +0000140
Tarek Ziadé667882f2010-02-11 20:47:18 +0000141 If *name* is not found, return ``None``.
Tarek Ziadé18755702010-02-02 23:17:47 +0000142
143
144.. function:: get_paths([scheme, [vars, [expand]]])
145
Tarek Ziadé667882f2010-02-11 20:47:18 +0000146 Return a dictionary containing all installation paths corresponding to an
Tarek Ziadé18755702010-02-02 23:17:47 +0000147 installation scheme. See :func:`get_path` for more information.
148
149 If *scheme* is not provided, will use the default scheme for the current
150 platform.
151
Tarek Ziadé667882f2010-02-11 20:47:18 +0000152 If *vars* is provided, it must be a dictionary of variables that will
153 update the dictionary used to expand the paths.
Tarek Ziadé18755702010-02-02 23:17:47 +0000154
155 If *expand* is set to False, the paths will not be expanded.
156
157 If *scheme* is not an existing scheme, :func:`get_paths` will raise a
158 :exc:`KeyError`.
159
160
161Other functions
162---------------
163
164.. function:: get_python_version()
165
Tarek Ziadé667882f2010-02-11 20:47:18 +0000166 Return the ``MAJOR.MINOR`` Python version number as a string. Similar to
Tarek Ziadé18755702010-02-02 23:17:47 +0000167 ``sys.version[:3]``.
168
Tarek Ziadé667882f2010-02-11 20:47:18 +0000169
Tarek Ziadé18755702010-02-02 23:17:47 +0000170.. function:: get_platform()
171
172 Return a string that identifies the current platform.
173
174 This is used mainly to distinguish platform-specific build directories and
Tarek Ziadé667882f2010-02-11 20:47:18 +0000175 platform-specific built distributions. Typically includes the OS name and
176 version and the architecture (as supplied by :func:`os.uname`), although the
177 exact information included depends on the OS; e.g. for IRIX the architecture
178 isn't particularly important (IRIX only runs on SGI hardware), but for Linux
179 the kernel version isn't particularly important.
Tarek Ziadé18755702010-02-02 23:17:47 +0000180
181 Examples of returned values:
182
183 - linux-i586
184 - linux-alpha (?)
185 - solaris-2.6-sun4u
186 - irix-5.3
187 - irix64-6.2
188
189 Windows will return one of:
190
191 - win-amd64 (64bit Windows on AMD64 (aka x86_64, Intel64, EM64T, etc)
192 - win-ia64 (64bit Windows on Itanium)
193 - win32 (all others - specifically, sys.platform is returned)
194
Tarek Ziadé667882f2010-02-11 20:47:18 +0000195 Mac OS X can return:
Tarek Ziadé18755702010-02-02 23:17:47 +0000196
197 - macosx-10.6-ppc
198 - macosx-10.4-ppc64
199 - macosx-10.3-i386
200 - macosx-10.4-fat
201
Tarek Ziadé667882f2010-02-11 20:47:18 +0000202 For other non-POSIX platforms, currently just returns :data:`sys.platform`.
Tarek Ziadé18755702010-02-02 23:17:47 +0000203
204
Tarek Ziadé667882f2010-02-11 20:47:18 +0000205.. function:: is_python_build()
Tarek Ziadé18755702010-02-02 23:17:47 +0000206
Tarek Ziadé667882f2010-02-11 20:47:18 +0000207 Return ``True`` if the current Python installation was built from source.
Tarek Ziadé18755702010-02-02 23:17:47 +0000208
209
Tarek Ziadé667882f2010-02-11 20:47:18 +0000210.. function:: parse_config_h(fp[, vars])
Tarek Ziadé18755702010-02-02 23:17:47 +0000211
Tarek Ziadé667882f2010-02-11 20:47:18 +0000212 Parse a :file:`config.h`\-style file.
Tarek Ziadé18755702010-02-02 23:17:47 +0000213
Tarek Ziadé667882f2010-02-11 20:47:18 +0000214 *fp* is a file-like object pointing to the :file:`config.h`\-like file.
Tarek Ziadé18755702010-02-02 23:17:47 +0000215
216 A dictionary containing name/value pairs is returned. If an optional
Tarek Ziadé667882f2010-02-11 20:47:18 +0000217 dictionary is passed in as the second argument, it is used instead of a new
218 dictionary, and updated with the values read in the file.
Tarek Ziadé18755702010-02-02 23:17:47 +0000219
220
Tarek Ziadé667882f2010-02-11 20:47:18 +0000221.. function:: get_config_h_filename()
Tarek Ziadé18755702010-02-02 23:17:47 +0000222
Tarek Ziadé667882f2010-02-11 20:47:18 +0000223 Return the path of :file:`pyconfig.h`.
Tarek Ziadéa7514992010-05-25 09:44:36 +0000224
Barry Warsawebbef6f2010-09-20 15:29:53 +0000225.. function:: get_makefile_filename()
226
227 Return the path of :file:`Makefile`.
228
Tarek Ziadéa7514992010-05-25 09:44:36 +0000229Using :mod:`sysconfig` as a script
230----------------------------------
231
232You can use :mod:`sysconfig` as a script with Python's *-m* option::
233
234 $ python -m sysconfig
235 Platform: "macosx-10.4-i386"
236 Python version: "3.2"
237 Current installation scheme: "posix_prefix"
238
239 Paths:
240 data = "/usr/local"
241 include = "/Users/tarek/Dev/svn.python.org/py3k/Include"
242 platinclude = "."
243 platlib = "/usr/local/lib/python3.2/site-packages"
244 platstdlib = "/usr/local/lib/python3.2"
245 purelib = "/usr/local/lib/python3.2/site-packages"
246 scripts = "/usr/local/bin"
247 stdlib = "/usr/local/lib/python3.2"
248
249 Variables:
250 AC_APPLE_UNIVERSAL_BUILD = "0"
251 AIX_GENUINE_CPLUSPLUS = "0"
252 AR = "ar"
253 ARFLAGS = "rc"
254 ASDLGEN = "./Parser/asdl_c.py"
255 ...
256
257This call will print in the standard output the information returned by
258:func:`get_platform`, :func:`get_python_version`, :func:`get_path` and
259:func:`get_config_vars`.