2

I have a what looks like this in some directory.

    .
├── Makefile
├── mod
│   ├── advancedmath.cpp
│   ├── advancedmath.py
│   ├── __init__.cpp
│   ├── __init__.py
│   ├── main.cpp
│   ├── main.pxd
│   ├── main.pyx
│   ├── mathic.pxd
│   └── mathic.pyx
├── run.py
├── setup.py
└── test.pyx

My setup.py looks like so:

from setuptools import setup, Extension
from Cython.Build import cythonize
from Cython.Compiler import Options as cyopts
from glob import glob
from pathlib import Path


cyopts.cimport_from_pyx = True


def mkcythonexts(base, allowed_ext={'.pyx', '.py'}, allow_cwd=False, 
                 exclude={'setup.py'}):
    '''
    given a base directory, constructs a cython module for each source file
    found within, of a matching extension type
    '''
    base = Path(base)
    exts = []
    for src in base.rglob('*.py*'):
        if src.suffix not in allowed_ext: continue
        name = str(src.parent) + '.' + src.stem
        if allow_cwd and src.parent == base:
            name = src.stem
        elif src.parent == base: continue
        if str(src) in exclude: continue
        print('INFO found matching source', src, name)
        exts += [Extension(name, [str(src)], language='c++')]
    return cythonize(exts, annotate=False)

setup(
        name = None,
        ext_modules = mkcythonexts(Path('.'), allow_cwd=True,
                                   exclude=['setup.py']),
        include_dirs=['.']
    )

What I'm doing is compiling each *.pyx file in mod/ as well as in . using Cython. My run.py file is pure-python, and it includes a call to the body of test.pyx.

All of this works fine with python3 setup.py build_ext --inplace. However, when I modify my cython call like so:

return cythonize(exts, annotate=False,
                         compiler_directives={'language_level': '3'})

the compiler complains:

Compiling run.py because it changed.
Compiling test.pyx because it changed.
Compiling mod/main.pyx because it changed.
Compiling mod/mathic.pyx because it changed.
[1/4] Cythonizing mod/main.pyx

Error compiling Cython file:
------------------------------------------------------------
...

from __future__ import print_function
cimport mathic
       ^
------------------------------------------------------------

mod/main.pyx:3:8: 'mathic.pxd' not found
Traceback (most recent call last):
  File "setup.py", line 34, in <module>
    exclude=['setup.py']),
  File "setup.py", line 29, in mkcythonexts
    compiler_directives={'language_level': '3'})
  File "/home/?/.local/lib/python3.5/site-packages/Cython/Build/Dependencies.py", line 1096, in cythonize
    cythonize_one(*args)
  File "/home/?/.local/lib/python3.5/site-packages/Cython/Build/Dependencies.py", line 1219, in cythonize_one
    raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: mod/main.pyx

Seemingly, everything breaks just because I request python3 syntax. I do not understand what's causing this. I tried playing with making it a kwarg, language_level= to cythonize instead of a compiler directive, with no luck. Removing that bit simply makes the error go away.

axolotl
  • 1,042
  • 1
  • 12
  • 23

0 Answers0