6

I am trying to compile a simple cython module using the following setup.py:

from distutils.core import setup from Cython.Build import cythonize setup( ext_modules=cythonize("verifier_c.pyx"), ) 

I have the following folder structure:

. c_ext/ __init__.py verifier_c.pyx setup.py 

If I run the following:

python setup.py build_ext --inplace 

I get an extra c_ext subfolder like this:

. c_ext/ build/ ... c_ext/ verifier_c.so __init__.py verifier_c.pyx setup.py 

But if I remove the __init__.py file, I get the verifier_c.so file in the same folder as verifier_c.pyx.

I did not find where this behavior is documented, but I would like to keep verifier_c.so in the same folder as verifier_c.pyx but without having to delete __init__.py each time I run setup.py. How can I achieve that?

3
  • The setup.py is in the wrong folder. Move it up one level and change the argument of cythonize() accordingly. Then the .so file should show up in the already existing c_ext/ folder (which is a package if there is a __init__.py file present. Commented Jan 7, 2016 at 15:30
  • @BlackJack It is not possible to have the setup.py and .pyx file in the same folder and generate the .so in this folder? I would like to totally separate the c part from the reste of my app (and thus not put the setup.py in the base app). Commented Jan 7, 2016 at 18:26
  • I don't know if it's possible somehow but it is just wrong to have the setup.py in that spot. It's part of the package the way you organized it. There is a c_ext.setup module this way which doesn't make sense. Commented Jan 8, 2016 at 21:05

1 Answer 1

4

As mentioned in the comments, the setup.py should not live inside your package. As far as I know the build_ext commands has no option (apart from --inplace) to specify a target path. You can find some documentation here. Also this question deals with a similar topic.

To adapts the required package structure your package would have to look like:

c_ext/ setup.py myfile.py verifier/ __init__.py verifier_c.pyx 

You will get an extension that lives in the verifier package:

me@machine:~/c_ext/$ python setup.py build_ext --inplace c_ext/ setup.py myfile.py verifier/ __init__.py verifier_c.pyx verifier_c.so 

You can then import verifier_c from the verifier package. For example from myfile.py this would look like:

from verifier import verifier_c ... 

You can manage a separate package (and folder) for each Cython extension or create one sub folder that contains all of them. You have to pass the other modules to cythonize as well. It can handle a glob pattern, list of glob patterns or a list of Distutils.Extensions objects. The latter can be handy to specify cython compiler directives

from distutils.core import setup from distutils.extension import Extension from Cython.Build import cythonize extensions = [ Extension("verifier_c", ["verifier/verifier_c.pyx"]), Extension("something_else", ["foobar/something_else.pyx"] compiler_directives={'embedsignature': True}), ] setup( ext_modules=cythonize(extensions), ) 

I hope this help :)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.