0

I know how to embed the Python interpreter in my script.c script when using cython on the command line per this SO post.

However, unfortunately I'm getting the following errors in the headers of my .c file.

/* Generated by Cython 0.29.6 */

#define PY_SSIZE_T_CLEAN
#include "Python.h"
#ifndef Py_PYTHON_H
    #error Python headers needed to compile C extensions, please install development version of Python.
#elif PY_VERSION_HEX < 0x02060000 || (0x03000000 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x03030000)
    #error Cython requires Python 2.6+ or Python 3.3+.
#else

Since I'm using MacOS, I installed python via homebrew so I also know that my headers are located in the following directory.

/usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/Headers

With the knowledge of where the Python headers live, how do I fix my error when using cython to compile my Python code in C code so that it becomes a portable executable?

ptk
  • 6,835
  • 14
  • 45
  • 91
  • 1
    Those "following errors" aren't errors, they're a header file that shows two possible errors :-) Which errors are you actually getting? – paxdiablo Apr 05 '19 at 02:46
  • I get “cannot find python.h” when trying to run the c file which I’ve narrowed down to the headers issue but I’m unsure how to fix it now that I know where the headers reside :( – ptk Apr 05 '19 at 02:47
  • 1
    Ah, so it's *neither* of those errors, just the fact you can't locate `Python.h` :-) Don't know enough about Mac but, for `gcc` et al, you would just add the path with something like `gcc -I /pat/to/python ...`. – paxdiablo Apr 05 '19 at 02:52
  • Ahhh I see - I guess what I’m trying to achieve is to have a single .c file that has the python interpreter embedded in it. I’m using the embed flag in Cython to achieve this but based on the headers in the .c file, cython can’t locate those headers and so it’s being left out? Need it included but don’t know how :( – ptk Apr 05 '19 at 02:55
  • So, if you're using `gcc` (or `clang` I suspect, even if Apple have *named* it `gcc`), you just need to find a way to change the command to `gcc -I /usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/Headers `. How exactly are you building your stuff? – paxdiablo Apr 05 '19 at 03:03
  • So I just tried `gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing \ -I/usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/Headers -o ConnectFour.so ConnectFour.c` and now I get Undefined symbols for architecture x86_64: as the error. I guess that's good news that the header issue is resolved? Still not compiling my python script to C tho :( I figured out what cython is doing on [this](https://cython.readthedocs.io/en/latest/src/userguide/source_files_and_compilation.html) page of their docs. – ptk Apr 05 '19 at 03:18

2 Answers2

0

Do you have the python devs installed?
Linux:

sudo apt-get install python python-dev python-all python-all-dev

Edit: Didnt see Mac sorry. You may need python-devel, although brew has the header files aswell:

brew reinstall python
Comet
  • 260
  • 2
  • 8
  • I installed python via homebrew (on mac) and based on all the SO posts I've read, the header files for me are located in /usr/local/Cellar/python3/3.7.0/Frameworks/Python.framework/Versions/3.7/Headers which I've verified to be the case. As a side note, I can't actually install python-dev on OSX :L – ptk Apr 05 '19 at 03:37
  • Cython requires Python 2.6+ or Python 3.3+. May be that your using 3.7 – Comet Apr 05 '19 at 03:42
0

I was having the exact same issues (first complaining about Python.h, then saying Undefined symbols for architecture x86_64).

The way I solved it was by using the command cythonize -i hello_world.pyx (instead of cython followed by gcc).

Sidenote: if you're using c++ instead of c, the command cythonize expects your .pyx file to start with the line:

# distutils: language = c++
Telho
  • 21
  • 5