Take the 2-minute tour ×

I have a project consisting of several Cython modules in different folders:


In module/folder/file1.pxd I have something like this:

cdef extern from "../../clibs/File.hpp":
    cdef cppclass MyCppClass:
        int _data
        MyCppClass(int arg)

cdef class MyPyClass:
    cdef MyCppClass* cpp_obj

and then in module/file2.pyx I cimport the declarations from module/folder/file1.pxd:

from module.folder.file1 cimport MyCppClass, MyPyClass

Now, when I try to build this, cython generates the line

#include "../../clibs/File.hpp"

when reading module/folder/file1.pxd, and drops it into the newly generated module/file2.cpp, which is obviously an invalid path!

How do I resolve this? Can I specify a path relative to the project root in my extern clauses?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

My (temporary) solution to this is to create a symbolic link to the clibs directory in every python folder.

To stop this being a problem with version control, I've added the following to the top and the bottom of my setup.py:

# at the top:
import os
pwd = os.getcwd()

folders = [

for f in folders:
    os.system("ln -s " + pwd + "/clibs " + f + "/clibs")

    # setup commands...
    # used here so that if setup raises a compilation exception, we 
    # still tidy up:

    for f in folders:
        os.system("rm " + f + "/clibs")

notes: you need the full pwd - if you just specify the local folder name that's all that gets stored in the symlink, and you get an ELOOP too many symlinks error.

I still want a better solution to this, this is just a dirty hack, and only valid when ln -s and rm are there! If nothing else is posted, I'll eventually mark this as "accepted", but this shouldn't be necessary at all.

Update: Another bug is that when the code is on a networked filesystem (e.g. in a shared folder in VirtualBox) you are not allowed to create symlinks on the guest - very frustrating!

share|improve this answer

Your Answer


