| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 1 | :mod:`sysconfig` --- Provide access to Python's configuration information |
| 2 | ========================================================================= |
| 3 | |
| 4 | .. module:: sysconfig |
| 5 | :synopsis: Python's configuration information |
| Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 6 | |
| Éric Araujo | 19f9b71 | 2011-08-19 00:49:18 +0200 | [diff] [blame] | 7 | .. moduleauthor:: Tarek Ziadé <tarek@ziade.org> |
| 8 | .. sectionauthor:: Tarek Ziadé <tarek@ziade.org> |
| 9 | |
| Raymond Hettinger | a199368 | 2011-01-27 01:20:32 +0000 | [diff] [blame] | 10 | .. versionadded:: 3.2 |
| 11 | |
| Éric Araujo | 19f9b71 | 2011-08-19 00:49:18 +0200 | [diff] [blame] | 12 | **Source code:** :source:`Lib/sysconfig.py` |
| 13 | |
| Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 14 | .. index:: |
| 15 | single: configuration information |
| 16 | |
| Raymond Hettinger | a199368 | 2011-01-27 01:20:32 +0000 | [diff] [blame] | 17 | -------------- |
| 18 | |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 19 | The :mod:`sysconfig` module provides access to Python's configuration |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 20 | information like the list of installation paths and the configuration variables |
| 21 | relevant for the current platform. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 22 | |
| 23 | Configuration variables |
| 24 | ----------------------- |
| 25 | |
| Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 26 | A Python distribution contains a :file:`Makefile` and a :file:`pyconfig.h` |
| 27 | header file that are necessary to build both the Python binary itself and |
| 28 | third-party C extensions compiled using :mod:`distutils`. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 29 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 30 | :mod:`sysconfig` puts all variables found in these files in a dictionary that |
| 31 | can be accessed using :func:`get_config_vars` or :func:`get_config_var`. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 32 | |
| 33 | Notice that on Windows, it's a much smaller set. |
| 34 | |
| Andre Delfino | dcc997c | 2020-12-16 22:37:28 -0300 | [diff] [blame] | 35 | .. function:: get_config_vars(*args) |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 36 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 37 | With no arguments, return a dictionary of all configuration variables |
| 38 | relevant for the current platform. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 39 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 40 | With arguments, return a list of values that result from looking up each |
| 41 | argument in the configuration variable dictionary. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 42 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 43 | For each argument, if the value is not found, return ``None``. |
| 44 | |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 45 | |
| 46 | .. function:: get_config_var(name) |
| 47 | |
| 48 | Return the value of a single variable *name*. Equivalent to |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 49 | ``get_config_vars().get(name)``. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 50 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 51 | If *name* is not found, return ``None``. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 52 | |
| 53 | Example of usage:: |
| 54 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 55 | >>> 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é | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 62 | |
| 63 | |
| 64 | Installation paths |
| 65 | ------------------ |
| 66 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 67 | Python uses an installation scheme that differs depending on the platform and on |
| 68 | the installation options. These schemes are stored in :mod:`sysconfig` under |
| 69 | unique identifiers based on the value returned by :const:`os.name`. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 70 | |
| 71 | Every new component that is installed using :mod:`distutils` or a |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 72 | Distutils-based system will follow the same scheme to copy its file in the right |
| 73 | places. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 74 | |
| 75 | Python currently supports seven schemes: |
| 76 | |
| Miss Islington (bot) | 1493e1a | 2021-09-23 03:25:31 -0700 | [diff] [blame] | 77 | - *posix_prefix*: scheme for POSIX platforms like Linux or macOS. This is |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 78 | the default scheme used when Python or a component is installed. |
| Mathieu Dupuy | 6546056 | 2020-05-17 21:29:51 +0000 | [diff] [blame] | 79 | - *posix_home*: scheme for POSIX platforms used when a *home* option is used |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 80 | upon installation. This scheme is used when a component is installed through |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 81 | Distutils with a specific home prefix. |
| Mathieu Dupuy | 6546056 | 2020-05-17 21:29:51 +0000 | [diff] [blame] | 82 | - *posix_user*: scheme for POSIX platforms used when a component is installed |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 83 | through Distutils and the *user* option is used. This scheme defines paths |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 84 | located under the user home directory. |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 85 | - *nt*: scheme for NT platforms like Windows. |
| 86 | - *nt_user*: scheme for NT platforms, when the *user* option is used. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 87 | |
| 88 | Each scheme is itself composed of a series of paths and each path has a unique |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 89 | identifier. Python currently uses eight paths: |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 90 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 91 | - *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é | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 101 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 102 | :mod:`sysconfig` provides some functions to determine these paths. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 103 | |
| 104 | .. function:: get_scheme_names() |
| 105 | |
| 106 | Return a tuple containing all schemes currently supported in |
| 107 | :mod:`sysconfig`. |
| 108 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 109 | |
| Tzu-ping Chung | d925133 | 2021-04-27 16:45:55 +0800 | [diff] [blame] | 110 | .. function:: get_default_scheme() |
| 111 | |
| 112 | Return the default scheme name for the current platform. |
| 113 | |
| 114 | .. versionchanged:: 3.10 |
| 115 | This function was previously named ``_get_default_scheme()`` and |
| 116 | considered an implementation detail. |
| 117 | |
| 118 | |
| 119 | .. function:: get_preferred_scheme(key) |
| 120 | |
| 121 | Return a preferred scheme name for an installation layout specified by *key*. |
| 122 | |
| 123 | *key* must be either ``"prefix"``, ``"home"``, or ``"user"``. |
| 124 | |
| 125 | The return value is a scheme name listed in :func:`get_scheme_names`. It |
| 126 | can be passed to :mod:`sysconfig` functions that take a *scheme* argument, |
| 127 | such as :func:`get_paths`. |
| 128 | |
| 129 | .. versionadded:: 3.10 |
| 130 | |
| 131 | |
| 132 | .. function:: _get_preferred_schemes() |
| 133 | |
| 134 | Return a dict containing preferred scheme names on the current platform. |
| 135 | Python implementers and redistributors may add their preferred schemes to |
| 136 | the ``_INSTALL_SCHEMES`` module-level global value, and modify this function |
| 137 | to return those scheme names, to e.g. provide different schemes for system |
| 138 | and language package managers to use, so packages installed by either do not |
| 139 | mix with those by the other. |
| 140 | |
| 141 | End users should not use this function, but :func:`get_default_scheme` and |
| 142 | :func:`get_preferred_scheme()` instead. |
| 143 | |
| 144 | .. versionadded:: 3.10 |
| 145 | |
| 146 | |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 147 | .. function:: get_path_names() |
| 148 | |
| 149 | Return a tuple containing all path names currently supported in |
| 150 | :mod:`sysconfig`. |
| 151 | |
| 152 | |
| 153 | .. function:: get_path(name, [scheme, [vars, [expand]]]) |
| 154 | |
| 155 | Return an installation path corresponding to the path *name*, from the |
| 156 | install scheme named *scheme*. |
| 157 | |
| 158 | *name* has to be a value from the list returned by :func:`get_path_names`. |
| 159 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 160 | :mod:`sysconfig` stores installation paths corresponding to each path name, |
| 161 | for each platform, with variables to be expanded. For instance the *stdlib* |
| 162 | path for the *nt* scheme is: ``{base}/Lib``. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 163 | |
| 164 | :func:`get_path` will use the variables returned by :func:`get_config_vars` |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 165 | to expand the path. All variables have default values for each platform so |
| 166 | one may call this function and get the default value. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 167 | |
| 168 | If *scheme* is provided, it must be a value from the list returned by |
| Éric Araujo | 2bddc53 | 2011-11-29 16:34:58 +0100 | [diff] [blame] | 169 | :func:`get_scheme_names`. Otherwise, the default scheme for the current |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 170 | platform is used. |
| 171 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 172 | If *vars* is provided, it must be a dictionary of variables that will update |
| 173 | the dictionary return by :func:`get_config_vars`. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 174 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 175 | If *expand* is set to ``False``, the path will not be expanded using the |
| 176 | variables. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 177 | |
| Miss Islington (bot) | 5151826 | 2021-07-26 12:35:33 -0700 | [diff] [blame] | 178 | If *name* is not found, raise a :exc:`KeyError`. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 179 | |
| 180 | |
| 181 | .. function:: get_paths([scheme, [vars, [expand]]]) |
| 182 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 183 | Return a dictionary containing all installation paths corresponding to an |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 184 | installation scheme. See :func:`get_path` for more information. |
| 185 | |
| 186 | If *scheme* is not provided, will use the default scheme for the current |
| 187 | platform. |
| 188 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 189 | If *vars* is provided, it must be a dictionary of variables that will |
| 190 | update the dictionary used to expand the paths. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 191 | |
| Serhiy Storchaka | 4adf01c | 2016-10-19 18:30:05 +0300 | [diff] [blame] | 192 | If *expand* is set to false, the paths will not be expanded. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 193 | |
| 194 | If *scheme* is not an existing scheme, :func:`get_paths` will raise a |
| 195 | :exc:`KeyError`. |
| 196 | |
| 197 | |
| 198 | Other functions |
| 199 | --------------- |
| 200 | |
| 201 | .. function:: get_python_version() |
| 202 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 203 | Return the ``MAJOR.MINOR`` Python version number as a string. Similar to |
| Serhiy Storchaka | 885bdc4 | 2016-02-11 13:10:36 +0200 | [diff] [blame] | 204 | ``'%d.%d' % sys.version_info[:2]``. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 205 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 206 | |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 207 | .. function:: get_platform() |
| 208 | |
| 209 | Return a string that identifies the current platform. |
| 210 | |
| 211 | This is used mainly to distinguish platform-specific build directories and |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 212 | platform-specific built distributions. Typically includes the OS name and |
| Benjamin Peterson | 0693063 | 2017-09-04 16:36:05 -0700 | [diff] [blame] | 213 | version and the architecture (as supplied by 'os.uname()'), although the |
| 214 | exact information included depends on the OS; e.g., on Linux, the kernel |
| 215 | version isn't particularly important. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 216 | |
| 217 | Examples of returned values: |
| 218 | |
| 219 | - linux-i586 |
| 220 | - linux-alpha (?) |
| 221 | - solaris-2.6-sun4u |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 222 | |
| 223 | Windows will return one of: |
| 224 | |
| Andre Delfino | 55f41e4 | 2018-12-05 16:45:30 -0300 | [diff] [blame] | 225 | - win-amd64 (64bit Windows on AMD64, aka x86_64, Intel64, and EM64T) |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 226 | - win32 (all others - specifically, sys.platform is returned) |
| 227 | |
| Miss Islington (bot) | 1493e1a | 2021-09-23 03:25:31 -0700 | [diff] [blame] | 228 | macOS can return: |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 229 | |
| 230 | - macosx-10.6-ppc |
| 231 | - macosx-10.4-ppc64 |
| 232 | - macosx-10.3-i386 |
| 233 | - macosx-10.4-fat |
| 234 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 235 | For other non-POSIX platforms, currently just returns :data:`sys.platform`. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 236 | |
| 237 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 238 | .. function:: is_python_build() |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 239 | |
| Vinay Sajip | 37cac76 | 2016-08-25 15:13:24 +0100 | [diff] [blame] | 240 | Return ``True`` if the running Python interpreter was built from source and |
| 241 | is being run from its built location, and not from a location resulting from |
| 242 | e.g. running ``make install`` or installing via a binary installer. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 243 | |
| 244 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 245 | .. function:: parse_config_h(fp[, vars]) |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 246 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 247 | Parse a :file:`config.h`\-style file. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 248 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 249 | *fp* is a file-like object pointing to the :file:`config.h`\-like file. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 250 | |
| 251 | A dictionary containing name/value pairs is returned. If an optional |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 252 | dictionary is passed in as the second argument, it is used instead of a new |
| 253 | dictionary, and updated with the values read in the file. |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 254 | |
| 255 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 256 | .. function:: get_config_h_filename() |
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 257 | |
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 258 | Return the path of :file:`pyconfig.h`. |
| Tarek Ziadé | a751499 | 2010-05-25 09:44:36 +0000 | [diff] [blame] | 259 | |
| Barry Warsaw | ebbef6f | 2010-09-20 15:29:53 +0000 | [diff] [blame] | 260 | .. function:: get_makefile_filename() |
| 261 | |
| 262 | Return the path of :file:`Makefile`. |
| 263 | |
| Lumír 'Frenzy' Balhar | 90d02e5 | 2021-04-23 14:02:41 +0200 | [diff] [blame] | 264 | |
| Tarek Ziadé | a751499 | 2010-05-25 09:44:36 +0000 | [diff] [blame] | 265 | Using :mod:`sysconfig` as a script |
| 266 | ---------------------------------- |
| 267 | |
| Martin Panter | 1050d2d | 2016-07-26 11:18:21 +0200 | [diff] [blame] | 268 | You can use :mod:`sysconfig` as a script with Python's *-m* option: |
| 269 | |
| 270 | .. code-block:: shell-session |
| Tarek Ziadé | a751499 | 2010-05-25 09:44:36 +0000 | [diff] [blame] | 271 | |
| 272 | $ python -m sysconfig |
| 273 | Platform: "macosx-10.4-i386" |
| 274 | Python version: "3.2" |
| 275 | Current installation scheme: "posix_prefix" |
| 276 | |
| 277 | Paths: |
| 278 | data = "/usr/local" |
| 279 | include = "/Users/tarek/Dev/svn.python.org/py3k/Include" |
| 280 | platinclude = "." |
| 281 | platlib = "/usr/local/lib/python3.2/site-packages" |
| 282 | platstdlib = "/usr/local/lib/python3.2" |
| 283 | purelib = "/usr/local/lib/python3.2/site-packages" |
| 284 | scripts = "/usr/local/bin" |
| 285 | stdlib = "/usr/local/lib/python3.2" |
| 286 | |
| 287 | Variables: |
| 288 | AC_APPLE_UNIVERSAL_BUILD = "0" |
| 289 | AIX_GENUINE_CPLUSPLUS = "0" |
| 290 | AR = "ar" |
| 291 | ARFLAGS = "rc" |
| Tarek Ziadé | a751499 | 2010-05-25 09:44:36 +0000 | [diff] [blame] | 292 | ... |
| 293 | |
| 294 | This call will print in the standard output the information returned by |
| 295 | :func:`get_platform`, :func:`get_python_version`, :func:`get_path` and |
| 296 | :func:`get_config_vars`. |