简体   繁体   中英

Compiling C extensions for Python3 on CentOS

I need to write python interface wrappers around some C functions, which requires the Python.h header file. I want to make sure I get the correct Python.h.

I'm working in CentOS 6.6 and decided to use Python 3.4 so as to avoid using the OS's python distribution. Online sources suggest getting the correct Python.h from python34-devel , but this package is not available for Centos 6, even through the EPEL repository. Also, I was forced to compile and install python from source , and this thread seems to suggest python34-devel might not even be helpful in this case.

How do I find the right Python.h so that I can compile C libraries for my Python configuration?

thanks in advance

If you do things properly and make a complete installable package for your wrapper, with setup.py , then you do not even need to know the location of the includes; as your python3.4 executable would know their location.

Minimlly your setup.py could be something like

from setuptools import setup, Extension

c_ext = Extension('mypackage._mymodule.c',
                  sources = ['mypackage/_mymodule.c'],
                  )

setup(name='mypackage',
      version='1.0',
      description='...',
      long_description='...',
      packages=['mypackage']
      ext_modules=[c_ext])

Then you would store your C extension sources in mypackage/_mymodule.c , and have mypackage/__init__.py or some python modules write nice wrappers to the C extension itself (as some things are just to tedious to do it in C); minimally this would do

from mypackage._mymodule import *

Now to install this extension, you'd just execute python3.4 setup.py install and it would automatically compile your extension using whatever include directory and compile-time options are appropriate for that installation, and install it into the site-packages directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM