Skip to content Skip to sidebar Skip to footer

Return The Include And Runtime Lib Directories From Within Python

Lets say I want to use gcc from the command line in order to compile a C extension of Python. I'd structure the call something like this: gcc -o applesauce.pyd -I C:/Python35/inclu

Solution 1:

The easiest way to compile extensions is to use distutils, like this;

from distutils.coreimport setup
from distutils.extensionimportExtensionsetup(name='foobar',
      version='1.0',
      ext_modules=[Extension('foo', ['foo.c'])],
      )

Keep in mind that compared to unix/linux, compiling extensions on ms-windows is not straightforward because different versions of the ms compiler are tightly coupled with the corresponding C library. So on ms-windows you have to compile extensions for Python x.y with the same compiler that Python x.y was compiled with. This means using old and unsupported tools for e.g. Python 2.7, 3.3 and 3.4. The situation is changing (part 1, part 2) with Python 3.5.

Edit: Having looked in the problem more, I doubt that what you want is 100% achievable. Given that the tools needed for compiling and linking are by definition outside of Python and that they are not even necessarily the tools that Python was compiled with, the information in sys and sysconfig is not guaranteed to an accurate representation of the system that Python is actually installed on. E.g. most ms-windows machines will not have developer tools installed. And even on POSIX platforms there can be a difference between the installed C compiler and the compiler that built Python especially when it is installed as a binary package.

Solution 2:

If you look at the source of build_ext.py from distutils in the method finalize_options you will find code for different platforms used to locate libs.

Post a Comment for "Return The Include And Runtime Lib Directories From Within Python"