blob: 7ef3b2489673e6d72f11fb5eb59cde148a818fb4 [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
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Éric Araujo19f9b712011-08-19 00:49:18 +02007.. moduleauthor:: Tarek Ziadé <tarek@ziade.org>
8.. sectionauthor:: Tarek Ziadé <tarek@ziade.org>
9
Raymond Hettingera1993682011-01-27 01:20:32 +000010.. versionadded:: 3.2
11
Éric Araujo19f9b712011-08-19 00:49:18 +020012**Source code:** :source:`Lib/sysconfig.py`
13
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040014.. index::
15 single: configuration information
16
Raymond Hettingera1993682011-01-27 01:20:32 +000017--------------
18
Tarek Ziadé18755702010-02-02 23:17:47 +000019The :mod:`sysconfig` module provides access to Python's configuration
Tarek Ziadé667882f2010-02-11 20:47:18 +000020information like the list of installation paths and the configuration variables
21relevant for the current platform.
Tarek Ziadé18755702010-02-02 23:17:47 +000022
23Configuration variables
24-----------------------
25
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000026A Python distribution contains a :file:`Makefile` and a :file:`pyconfig.h`
27header file that are necessary to build both the Python binary itself and
28third-party C extensions compiled using :mod:`distutils`.
Tarek Ziadé18755702010-02-02 23:17:47 +000029
Tarek Ziadé667882f2010-02-11 20:47:18 +000030:mod:`sysconfig` puts all variables found in these files in a dictionary that
31can be accessed using :func:`get_config_vars` or :func:`get_config_var`.
Tarek Ziadé18755702010-02-02 23:17:47 +000032
33Notice that on Windows, it's a much smaller set.
34
Andre Delfinodcc997c2020-12-16 22:37:28 -030035.. function:: get_config_vars(*args)
Tarek Ziadé18755702010-02-02 23:17:47 +000036
Tarek Ziadé667882f2010-02-11 20:47:18 +000037 With no arguments, return a dictionary of all configuration variables
38 relevant for the current platform.
Tarek Ziadé18755702010-02-02 23:17:47 +000039
Tarek Ziadé667882f2010-02-11 20:47:18 +000040 With arguments, return a list of values that result from looking up each
41 argument in the configuration variable dictionary.
Tarek Ziadé18755702010-02-02 23:17:47 +000042
Tarek Ziadé667882f2010-02-11 20:47:18 +000043 For each argument, if the value is not found, return ``None``.
44
Tarek Ziadé18755702010-02-02 23:17:47 +000045
46.. function:: get_config_var(name)
47
48 Return the value of a single variable *name*. Equivalent to
Tarek Ziadé667882f2010-02-11 20:47:18 +000049 ``get_config_vars().get(name)``.
Tarek Ziadé18755702010-02-02 23:17:47 +000050
Tarek Ziadé667882f2010-02-11 20:47:18 +000051 If *name* is not found, return ``None``.
Tarek Ziadé18755702010-02-02 23:17:47 +000052
53Example of usage::
54
Tarek Ziadé667882f2010-02-11 20:47:18 +000055 >>> import sysconfig
56 >>> sysconfig.get_config_var('Py_ENABLE_SHARED')
57 0
58 >>> sysconfig.get_config_var('LIBDIR')
59 '/usr/local/lib'
60 >>> sysconfig.get_config_vars('AR', 'CXX')
61 ['ar', 'g++']
Tarek Ziadé18755702010-02-02 23:17:47 +000062
Miss Islington (bot)2a630e72021-11-18 13:46:06 -080063.. _installation_paths:
Tarek Ziadé18755702010-02-02 23:17:47 +000064
65Installation paths
66------------------
67
Tarek Ziadé667882f2010-02-11 20:47:18 +000068Python uses an installation scheme that differs depending on the platform and on
69the installation options. These schemes are stored in :mod:`sysconfig` under
70unique identifiers based on the value returned by :const:`os.name`.
Tarek Ziadé18755702010-02-02 23:17:47 +000071
72Every new component that is installed using :mod:`distutils` or a
Tarek Ziadé667882f2010-02-11 20:47:18 +000073Distutils-based system will follow the same scheme to copy its file in the right
74places.
Tarek Ziadé18755702010-02-02 23:17:47 +000075
Miss Islington (bot)2a630e72021-11-18 13:46:06 -080076Python currently supports six schemes:
Tarek Ziadé18755702010-02-02 23:17:47 +000077
Miss Islington (bot)1493e1a2021-09-23 03:25:31 -070078- *posix_prefix*: scheme for POSIX platforms like Linux or macOS. This is
Tarek Ziadé667882f2010-02-11 20:47:18 +000079 the default scheme used when Python or a component is installed.
Mathieu Dupuy65460562020-05-17 21:29:51 +000080- *posix_home*: scheme for POSIX platforms used when a *home* option is used
Tarek Ziadé667882f2010-02-11 20:47:18 +000081 upon installation. This scheme is used when a component is installed through
Tarek Ziadé18755702010-02-02 23:17:47 +000082 Distutils with a specific home prefix.
Mathieu Dupuy65460562020-05-17 21:29:51 +000083- *posix_user*: scheme for POSIX platforms used when a component is installed
Tarek Ziadé667882f2010-02-11 20:47:18 +000084 through Distutils and the *user* option is used. This scheme defines paths
Tarek Ziadé18755702010-02-02 23:17:47 +000085 located under the user home directory.
Tarek Ziadé667882f2010-02-11 20:47:18 +000086- *nt*: scheme for NT platforms like Windows.
87- *nt_user*: scheme for NT platforms, when the *user* option is used.
Miss Islington (bot)2a630e72021-11-18 13:46:06 -080088- *osx_framework_user*: scheme for macOS, when the *user* option is used.
Tarek Ziadé18755702010-02-02 23:17:47 +000089
90Each scheme is itself composed of a series of paths and each path has a unique
Tarek Ziadé667882f2010-02-11 20:47:18 +000091identifier. Python currently uses eight paths:
Tarek Ziadé18755702010-02-02 23:17:47 +000092
Tarek Ziadé667882f2010-02-11 20:47:18 +000093- *stdlib*: directory containing the standard Python library files that are not
94 platform-specific.
95- *platstdlib*: directory containing the standard Python library files that are
96 platform-specific.
97- *platlib*: directory for site-specific, platform-specific files.
98- *purelib*: directory for site-specific, non-platform-specific files.
99- *include*: directory for non-platform-specific header files.
100- *platinclude*: directory for platform-specific header files.
101- *scripts*: directory for script files.
102- *data*: directory for data files.
Tarek Ziadé18755702010-02-02 23:17:47 +0000103
Tarek Ziadé667882f2010-02-11 20:47:18 +0000104:mod:`sysconfig` provides some functions to determine these paths.
Tarek Ziadé18755702010-02-02 23:17:47 +0000105
106.. function:: get_scheme_names()
107
108 Return a tuple containing all schemes currently supported in
109 :mod:`sysconfig`.
110
Tarek Ziadé667882f2010-02-11 20:47:18 +0000111
Tzu-ping Chungd9251332021-04-27 16:45:55 +0800112.. function:: get_default_scheme()
113
114 Return the default scheme name for the current platform.
115
116 .. versionchanged:: 3.10
117 This function was previously named ``_get_default_scheme()`` and
118 considered an implementation detail.
119
120
121.. function:: get_preferred_scheme(key)
122
123 Return a preferred scheme name for an installation layout specified by *key*.
124
125 *key* must be either ``"prefix"``, ``"home"``, or ``"user"``.
126
127 The return value is a scheme name listed in :func:`get_scheme_names`. It
128 can be passed to :mod:`sysconfig` functions that take a *scheme* argument,
129 such as :func:`get_paths`.
130
131 .. versionadded:: 3.10
132
133
134.. function:: _get_preferred_schemes()
135
136 Return a dict containing preferred scheme names on the current platform.
137 Python implementers and redistributors may add their preferred schemes to
138 the ``_INSTALL_SCHEMES`` module-level global value, and modify this function
139 to return those scheme names, to e.g. provide different schemes for system
140 and language package managers to use, so packages installed by either do not
141 mix with those by the other.
142
143 End users should not use this function, but :func:`get_default_scheme` and
144 :func:`get_preferred_scheme()` instead.
145
146 .. versionadded:: 3.10
147
148
Tarek Ziadé18755702010-02-02 23:17:47 +0000149.. function:: get_path_names()
150
151 Return a tuple containing all path names currently supported in
152 :mod:`sysconfig`.
153
154
155.. function:: get_path(name, [scheme, [vars, [expand]]])
156
157 Return an installation path corresponding to the path *name*, from the
158 install scheme named *scheme*.
159
160 *name* has to be a value from the list returned by :func:`get_path_names`.
161
Tarek Ziadé667882f2010-02-11 20:47:18 +0000162 :mod:`sysconfig` stores installation paths corresponding to each path name,
163 for each platform, with variables to be expanded. For instance the *stdlib*
164 path for the *nt* scheme is: ``{base}/Lib``.
Tarek Ziadé18755702010-02-02 23:17:47 +0000165
166 :func:`get_path` will use the variables returned by :func:`get_config_vars`
Tarek Ziadé667882f2010-02-11 20:47:18 +0000167 to expand the path. All variables have default values for each platform so
168 one may call this function and get the default value.
Tarek Ziadé18755702010-02-02 23:17:47 +0000169
170 If *scheme* is provided, it must be a value from the list returned by
Éric Araujo2bddc532011-11-29 16:34:58 +0100171 :func:`get_scheme_names`. Otherwise, the default scheme for the current
Tarek Ziadé18755702010-02-02 23:17:47 +0000172 platform is used.
173
Tarek Ziadé667882f2010-02-11 20:47:18 +0000174 If *vars* is provided, it must be a dictionary of variables that will update
175 the dictionary return by :func:`get_config_vars`.
Tarek Ziadé18755702010-02-02 23:17:47 +0000176
Tarek Ziadé667882f2010-02-11 20:47:18 +0000177 If *expand* is set to ``False``, the path will not be expanded using the
178 variables.
Tarek Ziadé18755702010-02-02 23:17:47 +0000179
Miss Islington (bot)51518262021-07-26 12:35:33 -0700180 If *name* is not found, raise a :exc:`KeyError`.
Tarek Ziadé18755702010-02-02 23:17:47 +0000181
182
183.. function:: get_paths([scheme, [vars, [expand]]])
184
Tarek Ziadé667882f2010-02-11 20:47:18 +0000185 Return a dictionary containing all installation paths corresponding to an
Tarek Ziadé18755702010-02-02 23:17:47 +0000186 installation scheme. See :func:`get_path` for more information.
187
188 If *scheme* is not provided, will use the default scheme for the current
189 platform.
190
Tarek Ziadé667882f2010-02-11 20:47:18 +0000191 If *vars* is provided, it must be a dictionary of variables that will
192 update the dictionary used to expand the paths.
Tarek Ziadé18755702010-02-02 23:17:47 +0000193
Serhiy Storchaka4adf01c2016-10-19 18:30:05 +0300194 If *expand* is set to false, the paths will not be expanded.
Tarek Ziadé18755702010-02-02 23:17:47 +0000195
196 If *scheme* is not an existing scheme, :func:`get_paths` will raise a
197 :exc:`KeyError`.
198
199
200Other functions
201---------------
202
203.. function:: get_python_version()
204
Tarek Ziadé667882f2010-02-11 20:47:18 +0000205 Return the ``MAJOR.MINOR`` Python version number as a string. Similar to
Serhiy Storchaka885bdc42016-02-11 13:10:36 +0200206 ``'%d.%d' % sys.version_info[:2]``.
Tarek Ziadé18755702010-02-02 23:17:47 +0000207
Tarek Ziadé667882f2010-02-11 20:47:18 +0000208
Tarek Ziadé18755702010-02-02 23:17:47 +0000209.. function:: get_platform()
210
211 Return a string that identifies the current platform.
212
213 This is used mainly to distinguish platform-specific build directories and
Tarek Ziadé667882f2010-02-11 20:47:18 +0000214 platform-specific built distributions. Typically includes the OS name and
Benjamin Peterson06930632017-09-04 16:36:05 -0700215 version and the architecture (as supplied by 'os.uname()'), although the
216 exact information included depends on the OS; e.g., on Linux, the kernel
217 version isn't particularly important.
Tarek Ziadé18755702010-02-02 23:17:47 +0000218
219 Examples of returned values:
220
221 - linux-i586
222 - linux-alpha (?)
223 - solaris-2.6-sun4u
Tarek Ziadé18755702010-02-02 23:17:47 +0000224
225 Windows will return one of:
226
Andre Delfino55f41e42018-12-05 16:45:30 -0300227 - win-amd64 (64bit Windows on AMD64, aka x86_64, Intel64, and EM64T)
Tarek Ziadé18755702010-02-02 23:17:47 +0000228 - win32 (all others - specifically, sys.platform is returned)
229
Miss Islington (bot)1493e1a2021-09-23 03:25:31 -0700230 macOS can return:
Tarek Ziadé18755702010-02-02 23:17:47 +0000231
232 - macosx-10.6-ppc
233 - macosx-10.4-ppc64
234 - macosx-10.3-i386
235 - macosx-10.4-fat
236
Tarek Ziadé667882f2010-02-11 20:47:18 +0000237 For other non-POSIX platforms, currently just returns :data:`sys.platform`.
Tarek Ziadé18755702010-02-02 23:17:47 +0000238
239
Tarek Ziadé667882f2010-02-11 20:47:18 +0000240.. function:: is_python_build()
Tarek Ziadé18755702010-02-02 23:17:47 +0000241
Vinay Sajip37cac762016-08-25 15:13:24 +0100242 Return ``True`` if the running Python interpreter was built from source and
243 is being run from its built location, and not from a location resulting from
244 e.g. running ``make install`` or installing via a binary installer.
Tarek Ziadé18755702010-02-02 23:17:47 +0000245
246
Tarek Ziadé667882f2010-02-11 20:47:18 +0000247.. function:: parse_config_h(fp[, vars])
Tarek Ziadé18755702010-02-02 23:17:47 +0000248
Tarek Ziadé667882f2010-02-11 20:47:18 +0000249 Parse a :file:`config.h`\-style file.
Tarek Ziadé18755702010-02-02 23:17:47 +0000250
Tarek Ziadé667882f2010-02-11 20:47:18 +0000251 *fp* is a file-like object pointing to the :file:`config.h`\-like file.
Tarek Ziadé18755702010-02-02 23:17:47 +0000252
253 A dictionary containing name/value pairs is returned. If an optional
Tarek Ziadé667882f2010-02-11 20:47:18 +0000254 dictionary is passed in as the second argument, it is used instead of a new
255 dictionary, and updated with the values read in the file.
Tarek Ziadé18755702010-02-02 23:17:47 +0000256
257
Tarek Ziadé667882f2010-02-11 20:47:18 +0000258.. function:: get_config_h_filename()
Tarek Ziadé18755702010-02-02 23:17:47 +0000259
Tarek Ziadé667882f2010-02-11 20:47:18 +0000260 Return the path of :file:`pyconfig.h`.
Tarek Ziadéa7514992010-05-25 09:44:36 +0000261
Barry Warsawebbef6f2010-09-20 15:29:53 +0000262.. function:: get_makefile_filename()
263
264 Return the path of :file:`Makefile`.
265
Lumír 'Frenzy' Balhar90d02e52021-04-23 14:02:41 +0200266
Tarek Ziadéa7514992010-05-25 09:44:36 +0000267Using :mod:`sysconfig` as a script
268----------------------------------
269
Martin Panter1050d2d2016-07-26 11:18:21 +0200270You can use :mod:`sysconfig` as a script with Python's *-m* option:
271
272.. code-block:: shell-session
Tarek Ziadéa7514992010-05-25 09:44:36 +0000273
274 $ python -m sysconfig
275 Platform: "macosx-10.4-i386"
276 Python version: "3.2"
277 Current installation scheme: "posix_prefix"
278
279 Paths:
280 data = "/usr/local"
281 include = "/Users/tarek/Dev/svn.python.org/py3k/Include"
282 platinclude = "."
283 platlib = "/usr/local/lib/python3.2/site-packages"
284 platstdlib = "/usr/local/lib/python3.2"
285 purelib = "/usr/local/lib/python3.2/site-packages"
286 scripts = "/usr/local/bin"
287 stdlib = "/usr/local/lib/python3.2"
288
289 Variables:
290 AC_APPLE_UNIVERSAL_BUILD = "0"
291 AIX_GENUINE_CPLUSPLUS = "0"
292 AR = "ar"
293 ARFLAGS = "rc"
Tarek Ziadéa7514992010-05-25 09:44:36 +0000294 ...
295
296This call will print in the standard output the information returned by
297:func:`get_platform`, :func:`get_python_version`, :func:`get_path` and
298:func:`get_config_vars`.