'Import *.pyd file which has another imported file from build-lib

I'm trying to cythonize the python files in the mentioned folder structure to my_build and import the pyd file in test.py.

Folder structure:

|main_folder
|__,setup.py
|__,test.py
|__,src
|____,sample.py
|____sub_folder
     |__,fibo.py

fibo.py

def fib(n):
    a, b = 0, 1
    while b < n:
        print(b)
        a, b = b, a + b

sample.py

import sub_folder.fibo as fibo

class sample:
    def hello():
        print("Hello")
        fibo.fib(2000)

setup.py

from setuptools import setup, Extension
from Cython.Build import cythonize

ext_modules = extensions = [
    Extension("sub_folder.fibo", ["sub_folder/fibo.py"]),
    Extension("sample", ["sample.py"])
]

setup(
    ext_modules = cythonize(ext_modules, 
        build_dir="build_cfiles")
)

test.py

import my_build.sample as sa

sa.sample.hello()

The command for building:

python setup.py build_ext --build-lib my_build

Error:

...
import my_build.sample
  File "sample.py", line 2, in init sample
ModuleNotFoundError: No module named 'sub_folder'

This error is coming while executing the test.py file. So how fibo.py can be imported to get the proper result?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source