Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'cannot find cimported module' with subfolder #1718

Closed
xatian opened this issue May 27, 2017 · 2 comments
Closed

'cannot find cimported module' with subfolder #1718

xatian opened this issue May 27, 2017 · 2 comments

Comments

@xatian
Copy link

xatian commented May 27, 2017

I basically got these files:
src/header.pxd

cdef extern from "<stdio.h>":
    int printf (const char*, ...)

src/main.pyx

cimport header
def myFunc ():
    header.printf ("Hello World\n")

setup.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize

ext_modules = [
    Extension ("*", sources = ["src/main.pyx"], include_dirs = ['.'])
]
setup (
  name = "blabla",
  ext_modules = cythonize (ext_modules),
  script_args = ['build_ext'],
  options = {'build_ext': { 'inplace':True } }
)

Now if I run this I get this output:

>>> python setup.py
src/main.pyx: cannot find cimported module 'header'
running build_ext
building 'main' extension
gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Os -fomit-frame-pointer -fPIC -I. -I/usr/include/python3.5m -c src/main.c -o build/temp.linux-x86_64-3.5/src/main.o
gcc -shared -Wl,--as-needed build/temp.linux-x86_64-3.5/src/main.o -L/usr/lib -lpython3.5m -o /home/programming/python/main.cpython-35m-x86_64-linux-gnu.so

The warning makes no sense to me since importing the module and calling myFunc works fine.
If I put main.pyx and header.pxd in the same directory as setup.py the warning goes away.
What am I missing here?

If I add a empty __init__.py - file to the src-directory the warning goes away but then I cannot import any global module in main.pxy ... So when I add the init-file and change main.pxy to this

cimport header
import sys
def myFunc ():
    header.printf ("Hello World\n")
    print (sys.version)

I get this this error when importing the module:

Python 3.5.2 (default, Dec 22 2016, 10:15:38) 
[GCC 6.2.1 20160822] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import main
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "src/main.pyx", line 4, in init src.main (src/main.c:1050)
    import sys
SystemError: Parent module '' not loaded, cannot perform relative import
@Mekk
Copy link

Mekk commented Aug 27, 2018

I faced the same problem. Looks like cythonize assumes the package is layed out below setup.py, without interim directories (technically: adds just "." to its pxd search path to handle local pxd files).

There is simple workaround: add src to path yourself. In my case it was

   ext_modules=cythonize(
        …,
        # help cythonize find my own .pxd files
        include_path=[os.path.join(os.path.dirname(os.path.abspath(__file__)), "src")],
   )

(mayhaps bare ["src"] would suffiice but I opted to be safe and calculated absolute path)

@Mekk
Copy link

Mekk commented Aug 27, 2018

btw: in my case I inform setup about my layout by

    package_dir={'': "src"},

and of course at the time cythonize is called, this option is in no way available. Not sure whether pretty solution is possible (by pretty I mean handling this automatically, what would mean lazy-ing dependency calculation until setup object is constructed) but probably cythonize could handle some "where is top of my package" argument (defaulting to "." but allowing me to set it to "src" or whatever I use in given case)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants