Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cython compile module in separate directory

I am a newbie to cython.

I've the following directory structure.

cython_program/
cython_program/helloworld.py
cython_program/lib/printname.py

helloworld.py:

import lib.printname as name

def printname():
    name.myname()

printname.py:

def myname():
    print("this is my name")

setup.py:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [
    Extension("helloworld",  ["helloworld.py"]),
    Extension("mod",  ["./lib/printname.py"]),
]

setup(
    name = 'My Program',
    cmdclass = {'build_ext': build_ext},
    ext_modules = ext_modules
)

The problem I am having is that when I compile my program using python setup.py build_ext --inplace in the cython_program directory. It does compile the program successfully and generates a printname.c file in the lib folder.

But when I move the printname.py and helloworld.py to a separate folder to make sure that my cython compiled code is running. It gives me the following error ImportError: No module named lib.printname.

Why isn't it compiling the module(lib.printname) also with the main helloworld.py file ?

Note: This works fine if I keep both helloworld.py and printname.py in the same folder.

Thanks in advance.

like image 646
Mj1992 Avatar asked Jan 29 '23 10:01

Mj1992


1 Answers

It was a simple issue in setup.py.

Changed this line:

Extension("mod",  ["./lib/printname.py"]),

To This:

Extension("lib.printname",  ["./lib/printname.py"]),
like image 92
Mj1992 Avatar answered Feb 07 '23 11:02

Mj1992