| 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 | 
| Éric Araujo | 19f9b71 | 2011-08-19 00:49:18 +0200 | [diff] [blame] | 6 | .. moduleauthor:: Tarek Ziadé <tarek@ziade.org> | 
 | 7 | .. sectionauthor:: Tarek Ziadé <tarek@ziade.org> | 
 | 8 |  | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 9 | .. index:: | 
 | 10 |    single: configuration information | 
 | 11 |  | 
| Raymond Hettinger | a199368 | 2011-01-27 01:20:32 +0000 | [diff] [blame] | 12 | .. versionadded:: 3.2 | 
 | 13 |  | 
| Éric Araujo | 19f9b71 | 2011-08-19 00:49:18 +0200 | [diff] [blame] | 14 | **Source code:** :source:`Lib/sysconfig.py` | 
 | 15 |  | 
| Raymond Hettinger | a199368 | 2011-01-27 01:20:32 +0000 | [diff] [blame] | 16 | -------------- | 
 | 17 |  | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 18 | The :mod:`sysconfig` module provides access to Python's configuration | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 19 | information like the list of installation paths and the configuration variables | 
 | 20 | relevant for the current platform. | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 21 |  | 
 | 22 | Configuration variables | 
 | 23 | ----------------------- | 
 | 24 |  | 
| Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 25 | A Python distribution contains a :file:`Makefile` and a :file:`pyconfig.h` | 
 | 26 | header file that are necessary to build both the Python binary itself and | 
 | 27 | third-party C extensions compiled using :mod:`distutils`. | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 28 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 29 | :mod:`sysconfig` puts all variables found in these files in a dictionary that | 
 | 30 | 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] | 31 |  | 
 | 32 | Notice that on Windows, it's a much smaller set. | 
 | 33 |  | 
 | 34 | .. function:: get_config_vars(\*args) | 
 | 35 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 36 |    With no arguments, return a dictionary of all configuration variables | 
 | 37 |    relevant for the current platform. | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 38 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 39 |    With arguments, return a list of values that result from looking up each | 
 | 40 |    argument in the configuration variable dictionary. | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 41 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 42 |    For each argument, if the value is not found, return ``None``. | 
 | 43 |  | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 44 |  | 
 | 45 | .. function:: get_config_var(name) | 
 | 46 |  | 
 | 47 |    Return the value of a single variable *name*. Equivalent to | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 48 |    ``get_config_vars().get(name)``. | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 49 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 50 |    If *name* is not found, return ``None``. | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 51 |  | 
 | 52 | Example of usage:: | 
 | 53 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 54 |    >>> import sysconfig | 
 | 55 |    >>> sysconfig.get_config_var('Py_ENABLE_SHARED') | 
 | 56 |    0 | 
 | 57 |    >>> sysconfig.get_config_var('LIBDIR') | 
 | 58 |    '/usr/local/lib' | 
 | 59 |    >>> sysconfig.get_config_vars('AR', 'CXX') | 
 | 60 |    ['ar', 'g++'] | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 61 |  | 
 | 62 |  | 
 | 63 | Installation paths | 
 | 64 | ------------------ | 
 | 65 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 66 | Python uses an installation scheme that differs depending on the platform and on | 
 | 67 | the installation options.  These schemes are stored in :mod:`sysconfig` under | 
 | 68 | unique identifiers based on the value returned by :const:`os.name`. | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 69 |  | 
 | 70 | Every new component that is installed using :mod:`distutils` or a | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 71 | Distutils-based system will follow the same scheme to copy its file in the right | 
 | 72 | places. | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 73 |  | 
 | 74 | Python currently supports seven schemes: | 
 | 75 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 76 | - *posix_prefix*: scheme for Posix platforms like Linux or Mac OS X.  This is | 
 | 77 |   the default scheme used when Python or a component is installed. | 
 | 78 | - *posix_home*: scheme for Posix platforms used when a *home* option is used | 
 | 79 |   upon installation.  This scheme is used when a component is installed through | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 80 |   Distutils with a specific home prefix. | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 81 | - *posix_user*: scheme for Posix platforms used when a component is installed | 
 | 82 |   through Distutils and the *user* option is used.  This scheme defines paths | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 83 |   located under the user home directory. | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 84 | - *nt*: scheme for NT platforms like Windows. | 
 | 85 | - *nt_user*: scheme for NT platforms, when the *user* option is used. | 
 | 86 | - *os2*: scheme for OS/2 platforms. | 
 | 87 | - *os2_home*: scheme for OS/2 patforms, when the *user* option is used. | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 88 |  | 
 | 89 | 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] | 90 | identifier.  Python currently uses eight paths: | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 91 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 92 | - *stdlib*: directory containing the standard Python library files that are not | 
 | 93 |   platform-specific. | 
 | 94 | - *platstdlib*: directory containing the standard Python library files that are | 
 | 95 |   platform-specific. | 
 | 96 | - *platlib*: directory for site-specific, platform-specific files. | 
 | 97 | - *purelib*: directory for site-specific, non-platform-specific files. | 
 | 98 | - *include*: directory for non-platform-specific header files. | 
 | 99 | - *platinclude*: directory for platform-specific header files. | 
 | 100 | - *scripts*: directory for script files. | 
 | 101 | - *data*: directory for data files. | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 102 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 103 | :mod:`sysconfig` provides some functions to determine these paths. | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 104 |  | 
 | 105 | .. function:: get_scheme_names() | 
 | 106 |  | 
 | 107 |    Return a tuple containing all schemes currently supported in | 
 | 108 |    :mod:`sysconfig`. | 
 | 109 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 110 |  | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 111 | .. function:: get_path_names() | 
 | 112 |  | 
 | 113 |    Return a tuple containing all path names currently supported in | 
 | 114 |    :mod:`sysconfig`. | 
 | 115 |  | 
 | 116 |  | 
 | 117 | .. function:: get_path(name, [scheme, [vars, [expand]]]) | 
 | 118 |  | 
 | 119 |    Return an installation path corresponding to the path *name*, from the | 
 | 120 |    install scheme named *scheme*. | 
 | 121 |  | 
 | 122 |    *name* has to be a value from the list returned by :func:`get_path_names`. | 
 | 123 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 124 |    :mod:`sysconfig` stores installation paths corresponding to each path name, | 
 | 125 |    for each platform, with variables to be expanded.  For instance the *stdlib* | 
 | 126 |    path for the *nt* scheme is: ``{base}/Lib``. | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 127 |  | 
 | 128 |    :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] | 129 |    to expand the path.  All variables have default values for each platform so | 
 | 130 |    one may call this function and get the default value. | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 131 |  | 
 | 132 |    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] | 133 |    :func:`get_scheme_names`.  Otherwise, the default scheme for the current | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 134 |    platform is used. | 
 | 135 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 136 |    If *vars* is provided, it must be a dictionary of variables that will update | 
 | 137 |    the dictionary return by :func:`get_config_vars`. | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 138 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 139 |    If *expand* is set to ``False``, the path will not be expanded using the | 
 | 140 |    variables. | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 141 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 142 |    If *name* is not found, return ``None``. | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 143 |  | 
 | 144 |  | 
 | 145 | .. function:: get_paths([scheme, [vars, [expand]]]) | 
 | 146 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 147 |    Return a dictionary containing all installation paths corresponding to an | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 148 |    installation scheme. See :func:`get_path` for more information. | 
 | 149 |  | 
 | 150 |    If *scheme* is not provided, will use the default scheme for the current | 
 | 151 |    platform. | 
 | 152 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 153 |    If *vars* is provided, it must be a dictionary of variables that will | 
 | 154 |    update the dictionary used to expand the paths. | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 155 |  | 
 | 156 |    If *expand* is set to False, the paths will not be expanded. | 
 | 157 |  | 
 | 158 |    If *scheme* is not an existing scheme, :func:`get_paths` will raise a | 
 | 159 |    :exc:`KeyError`. | 
 | 160 |  | 
 | 161 |  | 
 | 162 | Other functions | 
 | 163 | --------------- | 
 | 164 |  | 
 | 165 | .. function:: get_python_version() | 
 | 166 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 167 |    Return the ``MAJOR.MINOR`` Python version number as a string.  Similar to | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 168 |    ``sys.version[:3]``. | 
 | 169 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 170 |  | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 171 | .. function:: get_platform() | 
 | 172 |  | 
 | 173 |    Return a string that identifies the current platform. | 
 | 174 |  | 
 | 175 |    This is used mainly to distinguish platform-specific build directories and | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 176 |    platform-specific built distributions.  Typically includes the OS name and | 
 | 177 |    version and the architecture (as supplied by :func:`os.uname`), although the | 
 | 178 |    exact information included depends on the OS; e.g. for IRIX the architecture | 
 | 179 |    isn't particularly important (IRIX only runs on SGI hardware), but for Linux | 
 | 180 |    the kernel version isn't particularly important. | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 181 |  | 
 | 182 |    Examples of returned values: | 
 | 183 |  | 
 | 184 |    - linux-i586 | 
 | 185 |    - linux-alpha (?) | 
 | 186 |    - solaris-2.6-sun4u | 
 | 187 |    - irix-5.3 | 
 | 188 |    - irix64-6.2 | 
 | 189 |  | 
 | 190 |    Windows will return one of: | 
 | 191 |  | 
 | 192 |    - win-amd64 (64bit Windows on AMD64 (aka x86_64, Intel64, EM64T, etc) | 
 | 193 |    - win-ia64 (64bit Windows on Itanium) | 
 | 194 |    - win32 (all others - specifically, sys.platform is returned) | 
 | 195 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 196 |    Mac OS X can return: | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 197 |  | 
 | 198 |    - macosx-10.6-ppc | 
 | 199 |    - macosx-10.4-ppc64 | 
 | 200 |    - macosx-10.3-i386 | 
 | 201 |    - macosx-10.4-fat | 
 | 202 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 203 |    For other non-POSIX platforms, currently just returns :data:`sys.platform`. | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 204 |  | 
 | 205 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 206 | .. function:: is_python_build() | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 207 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 208 |    Return ``True`` if the current Python installation was built from source. | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 209 |  | 
 | 210 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 211 | .. function:: parse_config_h(fp[, vars]) | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 212 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 213 |    Parse a :file:`config.h`\-style file. | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 214 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 215 |    *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] | 216 |  | 
 | 217 |    A dictionary containing name/value pairs is returned.  If an optional | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 218 |    dictionary is passed in as the second argument, it is used instead of a new | 
 | 219 |    dictionary, and updated with the values read in the file. | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 220 |  | 
 | 221 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 222 | .. function:: get_config_h_filename() | 
| Tarek Ziadé | 1875570 | 2010-02-02 23:17:47 +0000 | [diff] [blame] | 223 |  | 
| Tarek Ziadé | 667882f | 2010-02-11 20:47:18 +0000 | [diff] [blame] | 224 |    Return the path of :file:`pyconfig.h`. | 
| Tarek Ziadé | a751499 | 2010-05-25 09:44:36 +0000 | [diff] [blame] | 225 |  | 
| Barry Warsaw | ebbef6f | 2010-09-20 15:29:53 +0000 | [diff] [blame] | 226 | .. function:: get_makefile_filename() | 
 | 227 |  | 
 | 228 |    Return the path of :file:`Makefile`. | 
 | 229 |  | 
| Tarek Ziadé | a751499 | 2010-05-25 09:44:36 +0000 | [diff] [blame] | 230 | Using :mod:`sysconfig` as a script | 
 | 231 | ---------------------------------- | 
 | 232 |  | 
 | 233 | You can use :mod:`sysconfig` as a script with Python's *-m* option:: | 
 | 234 |  | 
 | 235 |     $ python -m sysconfig | 
 | 236 |     Platform: "macosx-10.4-i386" | 
 | 237 |     Python version: "3.2" | 
 | 238 |     Current installation scheme: "posix_prefix" | 
 | 239 |  | 
 | 240 |     Paths: | 
 | 241 |             data = "/usr/local" | 
 | 242 |             include = "/Users/tarek/Dev/svn.python.org/py3k/Include" | 
 | 243 |             platinclude = "." | 
 | 244 |             platlib = "/usr/local/lib/python3.2/site-packages" | 
 | 245 |             platstdlib = "/usr/local/lib/python3.2" | 
 | 246 |             purelib = "/usr/local/lib/python3.2/site-packages" | 
 | 247 |             scripts = "/usr/local/bin" | 
 | 248 |             stdlib = "/usr/local/lib/python3.2" | 
 | 249 |  | 
 | 250 |     Variables: | 
 | 251 |             AC_APPLE_UNIVERSAL_BUILD = "0" | 
 | 252 |             AIX_GENUINE_CPLUSPLUS = "0" | 
 | 253 |             AR = "ar" | 
 | 254 |             ARFLAGS = "rc" | 
 | 255 |             ASDLGEN = "./Parser/asdl_c.py" | 
 | 256 |             ... | 
 | 257 |  | 
 | 258 | This call will print in the standard output the information returned by | 
 | 259 | :func:`get_platform`, :func:`get_python_version`, :func:`get_path` and | 
 | 260 | :func:`get_config_vars`. |