1

I'm trying to build a Python extension and package it up using distutils but the extension installs in the root package no matter how I name it. My directory layout looks like this:

foo/bar/extension.c

My setup.py looks like this:

from distutils.core import setup
from distutils.extension import Extension

setup(name='foo.bar.extension',
      cmdclass={'build_ext': build_ext},
      ext_modules=[Extension('foo.bar.extension',
                             sources=['foo/bar/extension.c'])]
)

I set up a virtualenv and run

python setup.py install

Then in my Python shell:

>>> import foo.bar.extension
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named invest_cython_core
>>> import extension #This works!

What can I change so that the first import statement to works and the second one to fails?

Rich
  • 12,068
  • 9
  • 62
  • 94

1 Answers1

1

I think you need to have foo/__init__.py and foo/bar/__init__.py so that distutils installs first these packages and then the extension module. (An error would be better than a silent misbehavior here, I shall open a bug report so that distutils2 behaves better.)

Are you using a custom build_ext class? (asking because of cmdclass={'build_ext': build_ext} in your example) That may play a part in the issue.

merwok
  • 6,779
  • 1
  • 28
  • 42
  • Thanks for the response. Yes, I hadn't realized it was important, but I'm using Cython's build_ext module. I don't know much about this stuff, but it seems that Cython doesn't behave well with nested extensions. I've since moved my extension to the root of the package and it works fine, although I would prefer it in a nested state. Is this: http://docs.python.org/distutils/index.html#distutils-index the sole reference I should be using to learn about compiled extensions? – Rich Jan 05 '12 at 21:20
  • Normally Cython’s build_ext class should just extend what distutils’ class does, so the distutils doc should be enough, but you never know. Have you tested my suggestion of creating the __init__.py files instead of moving the ext to the top level? – merwok Jan 06 '12 at 16:23
  • Yes, I originally had the project set up so the cython files were in a directory that also had an __init__.py. Is that what you mean? I also see on the Cython FAQ that you can't compile Cython with subpackages. I'm not sure if that's a description of what my problem was or not: http://wiki.cython.org/FAQ#HowtocompileCythonwithsubpackages – Rich Jan 10 '12 at 16:49
  • Yes, it looks like this is your problem. – merwok May 18 '12 at 02:20