YDW YDW - 2 months ago 9
Python Question

How to get the specific C compiler type from Python distutils?

I'd like to check the system's C compiler in Python so that I can add library links accordingly to compile my Cython code.

I understand

distutils.ccompiler.get_default_compiler()
or something like
compiler.compiler_type
would return a compiler name. But it is too coarse just like "unix", etc.

What I need is more specific information such as "gcc", "icc", "clang", etc., which are all shown as "unix" using the methods above.

One possible way to get the information is to check the system's environment variable
CC
via
os.environ["CC"]
, but it is not guaranteed that every system has
CC
defined so it is not a universal solution.

So, what should I do then? Thanks in advance!

Answer

Generally you should be able to use the platform module to get the info:

>>> import platform
>>> platform.python_compiler()
'GCC 4.8.5 20150623 (Red Hat 4.8.5-4)'