2

I am running code from main.py. Following is my directory structure:

I'm using Python 3.6 and Linux.

PackA
     PackB
          PackC
               _main.py
          PackD 
               cython_file.pyx
                         def func()   # function defined here. 
               cython_file.pxd
               __init__.py   
                     func()           # function call. 

The Package pyximport is installed used by Cython.

I'm importing the cython file in __init__.py using:

from cython_file import func

Error: ImportError: cannot import name 'func'

Where am I doing wrong?

user3924729
  • 29
  • 1
  • 4
  • 2
    In the absence of other evidence I'd suggest that `cython_file` doesn't have an importable Python object called `func`. – DavidW Sep 17 '20 at 06:47
  • I agree with @DavidW, this question needs a [mcve], otherwise nobody would be able to help. – ead Sep 17 '20 at 07:55
  • I'm also wondering if it needs relative imports (i.e. `from .cython_file import func`). I can never remember what's actually required though – DavidW Sep 17 '20 at 08:00
  • @DavidW I tried out relative imports suggested here but it is showing the error ```ModuelNotFoundError: No module named PartD.cython_file``` I have used ```import sys sys.path.append("..") ``` in ```_main.py```. – user3924729 Sep 18 '20 at 07:56
  • Instead of creating a function ```func``` in cython_file, if I create in another python file, the code works fine. – user3924729 Sep 18 '20 at 08:02
  • This really needs to be a complete example. You say you've used pyximport - we don't know where. You show one line of the pyx file and none of the pxd. There could be stuff in that affects it. – DavidW Sep 18 '20 at 08:08
  • I was not using pyximport initially. Now when I used: ``` import pyximport pyximport.install() ``` now it is showing ImportError: Building module PackD.cython_file failed: ["distutils.errors.CompileError: command 'C:\\\\Program Files (x86)\\\\Microsoft Visual Studio\\\\2019\\\\Community\\\\VC\\\\Tools\\\\MSVC\\\\14.24.28314\\\\bin\\\\HostX86\\\\x64\\\\cl.exe' failed with exit status 2\n"] – user3924729 Sep 20 '20 at 11:54
  • pyximport can hide some problems as well. You could start by compiling the cython file by hand and importing it by hand in a python terminal from the same directory: type `python` to start python, then in the python console `from cython_file import func`. – Pierre de Buyl Sep 22 '20 at 11:55

0 Answers0