0

I've created a simple .pyd file from my helloWorld.py script using the sample code from here (https://stackoverflow.com/a/36946412/8729576), and although it does generate the .pyd file (along with a build folder, helloWorld.c file) - it throws an error [ImportError: dynamic module does not define module export function (PyInit_helloWorld)] when I attempt to import the function defined inside the original helloWorld.py called printHW using the normal import syntax of:

from helloWorld import printHW
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define module export function (PyInit_helloWorld)

helloWorld.py

import time
def printHW():
    print("Hello World - today's date is %s"%time.strftime('%Y-%m-%d',time.localtime()))
if '__name__' == '__main__':
    printHW()

setup.py

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

ext_modules = [Extension("helloWorld",["helloWorld.py"]) ]
for e in ext_modules:
    e.cython_directives = {'language_level' : '3'}

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

and then in the command prompt enter:

python setup.py build_ext --inplace

I've never worked with Cython before so really not sure what I'm doing wrong here and most SO answers are specific and not generic to what I'm trying to understand (with this basic example).

12
  • Are you building it with Python 2 and running it with Python 3? I.e. is it a duplicate of stackoverflow.com/questions/29657319/… (with a slightly different method of building)? Commented Jan 4, 2023 at 7:37
  • Or stackoverflow.com/a/36727516/4657412 Commented Jan 4, 2023 at 7:40
  • @DavidW - no, the code is written in python 3 and Im building in python3 as well! Commented Jan 4, 2023 at 7:40
  • 1
    1) Are you renaming any files manually and not mentioning it in the question? 2) What is the exact name of the .pyd file generated? Commented Jan 4, 2023 at 8:38
  • 1
    The cythonizing step should take <1s. Definitely don't leave it overnight! I don't know what's going wrong but it should be quick. Commented Jan 4, 2023 at 22:18

1 Answer 1

0

For anyone else that comes across this error - it has to do with the name of your output file. Playing with the name of the output pyd file and the name provided in the setup.py <[Extension("ThisShallBeYourModuleImportName",["helloWorld.py"]) ]> line I was able to fix my issue (thanks to @DavidW). Observe that whatever name you provide to your .py script in the Extensions list is what you will have to import it as, it is case sensitive. Furthermore, to import the compiled .pyd file 'ThisShallBeYourModuleImportName.cp36-win_amd64.pyd' in your python script, all you need is to say import ThisShallBeYourModuleImportName in your python script and it shall import the module, additionally you can remove the .cp36-win_amd64 and it will still be imported successfully. Try building the above setup.py code with the following changes to observe what I've stated:

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

ext_modules = [Extension("jAckSparroW",["helloWorld.py"]) ]
for e in ext_modules:
    e.cython_directives = {'language_level' : '3'}

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

Now open terminal and try

import jacksparrow
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'jacksparrow'
import jAckSparroW
>>>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.