How to get the list of options that Python was compiled with?

PythonCompilationPackages

Python Problem Overview


You can compile Python in various ways. I'd like to find out with which options my Python was compiled.

Concrete use-case: was my Python compiled with readline? I know I can see this by doing "import readline", but I'd like to see a list of compilation setting for my Python binary.

Edit: I mean the Python executable and not source code written by myself.

Python Solutions


Solution 1 - Python

There is a module to see the system config

import sysconfig
print(sysconfig.get_config_vars())

It offers an interface to get individual variables as well.

sysconfig.get_config_var('HAVE_LIBREADLINE')

It is also accessible via shell:

python3 -m sysconfig | less

Edit:

before python2.7, you have to use

import distutils.sysconfig
print distutils.sysconfig.get_config_vars()

Solution 2 - Python

To build on mirk's answer, to find the configure flags that were actually used during the build, the value you're looking for is CONFIG_ARGS.

For example, this is the output for an Ubuntu-compiled Python:

>>> print distutils.sysconfig.get_config_var('CONFIG_ARGS')
'--enable-shared' '--prefix=/usr' '--enable-ipv6'
'--enable-unicode=ucs4' '--with-dbmliborder=bdb:gdbm'
'--with-system-expat' '--with-system-ffi' '--with-fpe ctl'
'CC=x86_64-linux-gnu-gcc' 'CFLAGS=-D_FORTIFY_SOURCE=2 -g
-fstack-protector --param=ssp-buffer-size=4 -Wformat
-Werror=format-security ' 'LDFLAGS=-Wl,-Bs ymbolic-functions
-Wl,-z,relro'

Solution 3 - Python

And another way to do it... Python supplies scripts per installed version...

  ls -l /usr/bin/python*config*
    16 Dec 21  2013 /usr/bin/python-config     -> python2.7-config
    16 Dec 21  2013 /usr/bin/python2-config    -> python2.7-config
    33 Mar 22 18:57 /usr/bin/python2.7-config  -> x86_64-linux-gnu-python2.7-config
    16 Mar 23 03:17 /usr/bin/python3-config    -> python3.4-config
    33 Apr 11 09:15 /usr/bin/python3.4-config  -> x86_64-linux-gnu-python3.4-config
    34 Apr 11 09:15 /usr/bin/python3.4m-config -> x86_64-linux-gnu-python3.4m-config
    17 Mar 23 03:17 /usr/bin/python3m-config   -> python3.4m-config

  python3-config --help
  Usage: /usr/bin/python3-config --prefix|--exec-prefix|--includes|--libs|--cflags|--ldflags|--extension-suffix|--help|--abiflags|--configdir

  python3-config --prefix
  /usr

The answers from one of my systems are:

--prefix           /usr
--exec-prefix      /usr
--includes         -I/usr/include/python3.4m -I/usr/include/python3.4m
--libs             -lpthread -ldl  -lutil -lm  -lpython3.4m
--cflags           -I/usr/include/python3.4m -I/usr/include/python3.4m  -Wno-unused-result -Werror=declaration-after-statement -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security  -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes
--ldflags          -L/usr/lib/python3.4/config-3.4m-x86_64-linux-gnu -L/usr/lib -lpthread -ldl  -lutil -lm  -lpython3.4m -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions
--extension-suffix @SO@
--abiflags         m
--configdir        /usr/lib/python3.4/config-3.4m-x86_64-linux-gnu

So if you need setting values for bash scripts and such, these are available with this command line utility.

Solution 4 - Python

Here is a command that i use to compare different python configurations. It includes getting the value of the outputs:

$ python3.6 -c "import sysconfig;print('{}'.format('\n'.join(['{} = {}'.format(v, sysconfig.get_config_var(v)) for v in sorted(sysconfig.get_config_vars(), key=lambda s: s.lower())])))" > /tmp/python36.conf

$ python2.7 -c "import sysconfig;print('{}'.format('\n'.join(['{} = {}'.format(v, sysconfig.get_config_var(v)) for v in sorted(sysconfig.get_config_vars(), key=lambda s: s.lower())])))" > /tmp/python27.conf

$ sdiff /tmp/python36.conf /tmp/python27.conf

$ # This is my own version of colorized side-by-side diff from
$ # https://github.com/jlinoff/csdiff
$ csdiff /tmp/python36.conf /tmp/python27.conf

Solution 5 - Python

(This is for reference only)

BTW, to get the list of options that PyPy was compiled with, run

pypy --info

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNiels BomView Question on Stackoverflow
Solution 1 - PythonmirkView Answer on Stackoverflow
Solution 2 - PythonMike JohnsonView Answer on Stackoverflow
Solution 3 - PythonShenmeView Answer on Stackoverflow
Solution 4 - PythonJoe LinoffView Answer on Stackoverflow
Solution 5 - PythonRockalliteView Answer on Stackoverflow