• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1:mod:`!sysconfig` --- Provide access to Python's configuration information
2==========================================================================
3
4.. module:: sysconfig
5   :synopsis: Python's configuration information
6
7.. moduleauthor:: Tarek Ziadé <tarek@ziade.org>
8.. sectionauthor:: Tarek Ziadé <tarek@ziade.org>
9
10.. versionadded:: 3.2
11
12**Source code:** :source:`Lib/sysconfig`
13
14.. index::
15   single: configuration information
16
17--------------
18
19The :mod:`sysconfig` module provides access to Python's configuration
20information like the list of installation paths and the configuration variables
21relevant for the current platform.
22
23
24Configuration variables
25-----------------------
26
27A Python distribution contains a :file:`Makefile` and a :file:`pyconfig.h`
28header file that are necessary to build both the Python binary itself and
29third-party C extensions compiled using ``setuptools``.
30
31:mod:`sysconfig` puts all variables found in these files in a dictionary that
32can be accessed using :func:`get_config_vars` or :func:`get_config_var`.
33
34Notice that on Windows, it's a much smaller set.
35
36.. function:: get_config_vars(*args)
37
38   With no arguments, return a dictionary of all configuration variables
39   relevant for the current platform.
40
41   With arguments, return a list of values that result from looking up each
42   argument in the configuration variable dictionary.
43
44   For each argument, if the value is not found, return ``None``.
45
46
47.. function:: get_config_var(name)
48
49   Return the value of a single variable *name*. Equivalent to
50   ``get_config_vars().get(name)``.
51
52   If *name* is not found, return ``None``.
53
54Example of usage::
55
56   >>> import sysconfig
57   >>> sysconfig.get_config_var('Py_ENABLE_SHARED')
58   0
59   >>> sysconfig.get_config_var('LIBDIR')
60   '/usr/local/lib'
61   >>> sysconfig.get_config_vars('AR', 'CXX')
62   ['ar', 'g++']
63
64
65.. _installation_paths:
66
67Installation paths
68------------------
69
70Python uses an installation scheme that differs depending on the platform and on
71the installation options.  These schemes are stored in :mod:`sysconfig` under
72unique identifiers based on the value returned by :const:`os.name`.
73The schemes are used by package installers to determine where to copy files to.
74
75Python currently supports nine schemes:
76
77- *posix_prefix*: scheme for POSIX platforms like Linux or macOS.  This is
78  the default scheme used when Python or a component is installed.
79- *posix_home*: scheme for POSIX platforms, when the *home* option is used.
80  This scheme defines paths located under a specific home prefix.
81- *posix_user*: scheme for POSIX platforms, when the *user* option is used.
82  This scheme defines paths located under the user's home directory
83  (:const:`site.USER_BASE`).
84- *posix_venv*: scheme for :mod:`Python virtual environments <venv>` on POSIX
85  platforms; by default it is the same as *posix_prefix*.
86- *nt*: scheme for Windows.
87  This is the default scheme used when Python or a component is installed.
88- *nt_user*: scheme for Windows, when the *user* option is used.
89- *nt_venv*: scheme for :mod:`Python virtual environments <venv>` on Windows;
90  by default it is the same as *nt*.
91- *venv*: a scheme with values from either *posix_venv* or *nt_venv* depending
92  on the platform Python runs on.
93- *osx_framework_user*: scheme for macOS, when the *user* option is used.
94
95Each scheme is itself composed of a series of paths and each path has a unique
96identifier.  Python currently uses eight paths:
97
98- *stdlib*: directory containing the standard Python library files that are not
99  platform-specific.
100- *platstdlib*: directory containing the standard Python library files that are
101  platform-specific.
102- *platlib*: directory for site-specific, platform-specific files.
103- *purelib*: directory for site-specific, non-platform-specific files ('pure' Python).
104- *include*: directory for non-platform-specific header files for
105  the Python C-API.
106- *platinclude*: directory for platform-specific header files for
107  the Python C-API.
108- *scripts*: directory for script files.
109- *data*: directory for data files.
110
111
112.. _sysconfig-user-scheme:
113
114User scheme
115---------------
116
117This scheme is designed to be the most convenient solution for users that don't
118have write permission to the global site-packages directory or don't want to
119install into it.
120
121Files will be installed into subdirectories of :const:`site.USER_BASE` (written
122as :file:`{userbase}` hereafter).  This scheme installs pure Python modules and
123extension modules in the same location (also known as :const:`site.USER_SITE`).
124
125``posix_user``
126^^^^^^^^^^^^^^
127
128============== ===========================================================
129Path           Installation directory
130============== ===========================================================
131*stdlib*       :file:`{userbase}/lib/python{X.Y}`
132*platstdlib*   :file:`{userbase}/lib/python{X.Y}`
133*platlib*      :file:`{userbase}/lib/python{X.Y}/site-packages`
134*purelib*      :file:`{userbase}/lib/python{X.Y}/site-packages`
135*include*      :file:`{userbase}/include/python{X.Y}`
136*scripts*      :file:`{userbase}/bin`
137*data*         :file:`{userbase}`
138============== ===========================================================
139
140``nt_user``
141^^^^^^^^^^^
142
143============== ===========================================================
144Path           Installation directory
145============== ===========================================================
146*stdlib*       :file:`{userbase}\\Python{XY}`
147*platstdlib*   :file:`{userbase}\\Python{XY}`
148*platlib*      :file:`{userbase}\\Python{XY}\\site-packages`
149*purelib*      :file:`{userbase}\\Python{XY}\\site-packages`
150*include*      :file:`{userbase}\\Python{XY}\\Include`
151*scripts*      :file:`{userbase}\\Python{XY}\\Scripts`
152*data*         :file:`{userbase}`
153============== ===========================================================
154
155``osx_framework_user``
156^^^^^^^^^^^^^^^^^^^^^^
157
158============== ===========================================================
159Path           Installation directory
160============== ===========================================================
161*stdlib*       :file:`{userbase}/lib/python`
162*platstdlib*   :file:`{userbase}/lib/python`
163*platlib*      :file:`{userbase}/lib/python/site-packages`
164*purelib*      :file:`{userbase}/lib/python/site-packages`
165*include*      :file:`{userbase}/include/python{X.Y}`
166*scripts*      :file:`{userbase}/bin`
167*data*         :file:`{userbase}`
168============== ===========================================================
169
170
171.. _sysconfig-home-scheme:
172
173Home scheme
174-----------
175
176The idea behind the "home scheme" is that you build and maintain a personal
177stash of Python modules.  This scheme's name is derived from the idea of a
178"home" directory on Unix, since it's not unusual for a Unix user to make their
179home directory have a layout similar to :file:`/usr/` or :file:`/usr/local/`.
180This scheme can be used by anyone, regardless of the operating system they
181are installing for.
182
183``posix_home``
184^^^^^^^^^^^^^^
185
186============== ===========================================================
187Path           Installation directory
188============== ===========================================================
189*stdlib*       :file:`{home}/lib/python`
190*platstdlib*   :file:`{home}/lib/python`
191*platlib*      :file:`{home}/lib/python`
192*purelib*      :file:`{home}/lib/python`
193*include*      :file:`{home}/include/python`
194*platinclude*  :file:`{home}/include/python`
195*scripts*      :file:`{home}/bin`
196*data*         :file:`{home}`
197============== ===========================================================
198
199
200.. _sysconfig-prefix-scheme:
201
202Prefix scheme
203-------------
204
205The "prefix scheme" is useful when you wish to use one Python installation to
206perform the build/install (i.e., to run the setup script), but install modules
207into the third-party module directory of a different Python installation (or
208something that looks like a different Python installation).  If this sounds a
209trifle unusual, it is---that's why the user and home schemes come before.  However,
210there are at least two known cases where the prefix scheme will be useful.
211
212First, consider that many Linux distributions put Python in :file:`/usr`, rather
213than the more traditional :file:`/usr/local`.  This is entirely appropriate,
214since in those cases Python is part of "the system" rather than a local add-on.
215However, if you are installing Python modules from source, you probably want
216them to go in :file:`/usr/local/lib/python2.{X}` rather than
217:file:`/usr/lib/python2.{X}`.
218
219Another possibility is a network filesystem where the name used to write to a
220remote directory is different from the name used to read it: for example, the
221Python interpreter accessed as :file:`/usr/local/bin/python` might search for
222modules in :file:`/usr/local/lib/python2.{X}`, but those modules would have to
223be installed to, say, :file:`/mnt/{@server}/export/lib/python2.{X}`.
224
225``posix_prefix``
226^^^^^^^^^^^^^^^^
227
228============== ==========================================================
229Path           Installation directory
230============== ==========================================================
231*stdlib*       :file:`{prefix}/lib/python{X.Y}`
232*platstdlib*   :file:`{prefix}/lib/python{X.Y}`
233*platlib*      :file:`{prefix}/lib/python{X.Y}/site-packages`
234*purelib*      :file:`{prefix}/lib/python{X.Y}/site-packages`
235*include*      :file:`{prefix}/include/python{X.Y}`
236*platinclude*  :file:`{prefix}/include/python{X.Y}`
237*scripts*      :file:`{prefix}/bin`
238*data*         :file:`{prefix}`
239============== ==========================================================
240
241``nt``
242^^^^^^
243
244============== ==========================================================
245Path           Installation directory
246============== ==========================================================
247*stdlib*       :file:`{prefix}\\Lib`
248*platstdlib*   :file:`{prefix}\\Lib`
249*platlib*      :file:`{prefix}\\Lib\\site-packages`
250*purelib*      :file:`{prefix}\\Lib\\site-packages`
251*include*      :file:`{prefix}\\Include`
252*platinclude*  :file:`{prefix}\\Include`
253*scripts*      :file:`{prefix}\\Scripts`
254*data*         :file:`{prefix}`
255============== ==========================================================
256
257
258Installation path functions
259---------------------------
260
261:mod:`sysconfig` provides some functions to determine these installation paths.
262
263.. function:: get_scheme_names()
264
265   Return a tuple containing all schemes currently supported in
266   :mod:`sysconfig`.
267
268
269.. function:: get_default_scheme()
270
271   Return the default scheme name for the current platform.
272
273   .. versionadded:: 3.10
274      This function was previously named ``_get_default_scheme()`` and
275      considered an implementation detail.
276
277   .. versionchanged:: 3.11
278      When Python runs from a virtual environment,
279      the *venv* scheme is returned.
280
281.. function:: get_preferred_scheme(key)
282
283   Return a preferred scheme name for an installation layout specified by *key*.
284
285   *key* must be either ``"prefix"``, ``"home"``, or ``"user"``.
286
287   The return value is a scheme name listed in :func:`get_scheme_names`. It
288   can be passed to :mod:`sysconfig` functions that take a *scheme* argument,
289   such as :func:`get_paths`.
290
291   .. versionadded:: 3.10
292
293   .. versionchanged:: 3.11
294      When Python runs from a virtual environment and ``key="prefix"``,
295      the *venv* scheme is returned.
296
297
298.. function:: _get_preferred_schemes()
299
300   Return a dict containing preferred scheme names on the current platform.
301   Python implementers and redistributors may add their preferred schemes to
302   the ``_INSTALL_SCHEMES`` module-level global value, and modify this function
303   to return those scheme names, to e.g. provide different schemes for system
304   and language package managers to use, so packages installed by either do not
305   mix with those by the other.
306
307   End users should not use this function, but :func:`get_default_scheme` and
308   :func:`get_preferred_scheme` instead.
309
310   .. versionadded:: 3.10
311
312
313.. function:: get_path_names()
314
315   Return a tuple containing all path names currently supported in
316   :mod:`sysconfig`.
317
318
319.. function:: get_path(name, [scheme, [vars, [expand]]])
320
321   Return an installation path corresponding to the path *name*, from the
322   install scheme named *scheme*.
323
324   *name* has to be a value from the list returned by :func:`get_path_names`.
325
326   :mod:`sysconfig` stores installation paths corresponding to each path name,
327   for each platform, with variables to be expanded.  For instance the *stdlib*
328   path for the *nt* scheme is: ``{base}/Lib``.
329
330   :func:`get_path` will use the variables returned by :func:`get_config_vars`
331   to expand the path.  All variables have default values for each platform so
332   one may call this function and get the default value.
333
334   If *scheme* is provided, it must be a value from the list returned by
335   :func:`get_scheme_names`.  Otherwise, the default scheme for the current
336   platform is used.
337
338   If *vars* is provided, it must be a dictionary of variables that will update
339   the dictionary returned by :func:`get_config_vars`.
340
341   If *expand* is set to ``False``, the path will not be expanded using the
342   variables.
343
344   If *name* is not found, raise a :exc:`KeyError`.
345
346
347.. function:: get_paths([scheme, [vars, [expand]]])
348
349   Return a dictionary containing all installation paths corresponding to an
350   installation scheme. See :func:`get_path` for more information.
351
352   If *scheme* is not provided, will use the default scheme for the current
353   platform.
354
355   If *vars* is provided, it must be a dictionary of variables that will
356   update the dictionary used to expand the paths.
357
358   If *expand* is set to false, the paths will not be expanded.
359
360   If *scheme* is not an existing scheme, :func:`get_paths` will raise a
361   :exc:`KeyError`.
362
363
364Other functions
365---------------
366
367.. function:: get_python_version()
368
369   Return the ``MAJOR.MINOR`` Python version number as a string.  Similar to
370   ``'%d.%d' % sys.version_info[:2]``.
371
372
373.. function:: get_platform()
374
375   Return a string that identifies the current platform.
376
377   This is used mainly to distinguish platform-specific build directories and
378   platform-specific built distributions.  Typically includes the OS name and
379   version and the architecture (as supplied by :func:`os.uname`), although the
380   exact information included depends on the OS; e.g., on Linux, the kernel
381   version isn't particularly important.
382
383   Examples of returned values:
384
385   - linux-i586
386   - linux-alpha (?)
387   - solaris-2.6-sun4u
388
389   Windows will return one of:
390
391   - win-amd64 (64bit Windows on AMD64, aka x86_64, Intel64, and EM64T)
392   - win32 (all others - specifically, sys.platform is returned)
393
394   macOS can return:
395
396   - macosx-10.6-ppc
397   - macosx-10.4-ppc64
398   - macosx-10.3-i386
399   - macosx-10.4-fat
400
401   For other non-POSIX platforms, currently just returns :data:`sys.platform`.
402
403
404.. function:: is_python_build()
405
406   Return ``True`` if the running Python interpreter was built from source and
407   is being run from its built location, and not from a location resulting from
408   e.g. running ``make install`` or installing via a binary installer.
409
410
411.. function:: parse_config_h(fp[, vars])
412
413   Parse a :file:`config.h`\-style file.
414
415   *fp* is a file-like object pointing to the :file:`config.h`\-like file.
416
417   A dictionary containing name/value pairs is returned.  If an optional
418   dictionary is passed in as the second argument, it is used instead of a new
419   dictionary, and updated with the values read in the file.
420
421
422.. function:: get_config_h_filename()
423
424   Return the path of :file:`pyconfig.h`.
425
426.. function:: get_makefile_filename()
427
428   Return the path of :file:`Makefile`.
429
430.. _sysconfig-cli:
431
432Using :mod:`sysconfig` as a script
433----------------------------------
434
435You can use :mod:`sysconfig` as a script with Python's *-m* option:
436
437.. code-block:: shell-session
438
439    $ python -m sysconfig
440    Platform: "macosx-10.4-i386"
441    Python version: "3.2"
442    Current installation scheme: "posix_prefix"
443
444    Paths:
445            data = "/usr/local"
446            include = "/Users/tarek/Dev/svn.python.org/py3k/Include"
447            platinclude = "."
448            platlib = "/usr/local/lib/python3.2/site-packages"
449            platstdlib = "/usr/local/lib/python3.2"
450            purelib = "/usr/local/lib/python3.2/site-packages"
451            scripts = "/usr/local/bin"
452            stdlib = "/usr/local/lib/python3.2"
453
454    Variables:
455            AC_APPLE_UNIVERSAL_BUILD = "0"
456            AIX_GENUINE_CPLUSPLUS = "0"
457            AR = "ar"
458            ARFLAGS = "rc"
459            ...
460
461This call will print in the standard output the information returned by
462:func:`get_platform`, :func:`get_python_version`, :func:`get_path` and
463:func:`get_config_vars`.
464