convert *.py to *.Pyd and getting a no module error in import?

76 Views Asked by At

I have a Cython file (main_file_001.pyx) located at d:/exe_project/all_files/python_files/. I am attempting to compile this Cython file into a Python extension module and import it into another program. However, I'm encountering difficulties in the import process.

try:
    from setuptools import setup
    from setuptools import Extension
except ImportError:
    from distutils.core import setup
    from distutils.extension import Extension

from Cython.Distutils import build_ext

pat = "d:/exe_project/all_files/python_files/main_file_001.pyx"
ext_modules = [Extension("my_code_cython", [pat])]

setup(
    name='Generic model class',
    cmdclass={'build_ext': build_ext},
    ext_modules=ext_modules
)

After running python setup.py build_ext --inplace, I find a compiled module named my_code_cython.cp37-win_amd64.pyd in the same directory.

When I try to import the compiled module using import my_code_cython, in another Python program, I receive a "no module found" error.

Expected Outcome:

I expect to successfully import and use the functions or classes defined in the Cython module in my Python program without any import errors.

Additional Information:

I'm using pycahrm community edition, windows 10,Cython version 3.0.0a11. The Python version associated with my Cython compilation and import attempts is Python 3.7. I've tried adding the directory containing the .pyd file to the PYTHONPATH, but the issue persists.

make_file_001.pyx code as below:

def say_hello_to():
    print("Hello ")

print sys.path produce the following result

['D:\\exe_project\\all_files\\python_files', 'D:\\exe_project', 'C:\\Program Files\\Python37\\python37.zip', 'C:\\Program Files\\Python37\\DLLs', 'C:\\Program Files\\Python37\\lib', 'C:\\Program Files\\Python37', 'D:\\exe_project\\lib\\site-packages', 'c:\\python\\DLLs']
0

There are 0 best solutions below