82

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.

Niels Bom
  • 8,728
  • 11
  • 46
  • 62
  • You don't generally work with compiled python bytecode - normally you work with python source files. This doesn't really make too much sense, could you give some more context? – Gareth Latty Apr 17 '12 at 14:07
  • 2
    Lattyware: I think the OP meant how python's executable was compiled, not the bytecode of modules – sinelaw Apr 17 '12 at 14:12
  • sinelaw is correct, I meant the Python executable, added for clarity – Niels Bom Apr 17 '12 at 14:29

5 Answers5

89

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()
user26742873
  • 919
  • 6
  • 21
mirk
  • 5,302
  • 3
  • 32
  • 49
  • (accidentally deleted my original comment) This works on 3.2 and higher (in python 3) or, apparently on 2.7 and higher (in python 2). Doesn't seem to work on my 2.6.7 – sinelaw Apr 17 '12 at 14:19
  • thanks, I have updated the answer for earlier python-versions. – mirk Apr 17 '12 at 14:21
  • pretty-print version: `python3.10 -c 'import sysconfig; import json; print(json.dumps(sysconfig.get_config_vars(), indent = 4))'` – ynn Jul 30 '23 at 03:36
34

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'
Mike Johnson
  • 715
  • 8
  • 5
12

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.

Shenme
  • 1,182
  • 1
  • 13
  • 12
  • 4
    This doesn't do what the OP asked for ("list of options that Python was compiled with"). According to its manpage, it will instead "output build options for python C/C++ extensions or embedding". The options necessary to build software compatible with a given Python interpreter are not necessarily the same as the options used to build that interpreter. More, they often differ significantly. For example, in this case, your Python interpreter was almost certainly *not* built with "-I/usr/include/python3.4m". – AnotherSmellyGeek Oct 03 '18 at 04:54
6

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
Joe Linoff
  • 761
  • 9
  • 13
  • 1
    Thanks for this, it helped me figure out why my 3.7.3 on an RPi was 5.5x slower than the stock 3.5.3 I also found it easier to sift through by adding | egrep "(CFLAGS | GOTOS)", as those were what I needed out of the files. – Stephan Garland Apr 30 '19 at 07:28
-2

(This is for reference only)

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

pypy --info
Rockallite
  • 16,437
  • 7
  • 54
  • 48