Pebaz / nimporter

Compile Nim Extensions for Python On Import!
MIT License
821 stars 33 forks source link

error message: dynamic module does not define module export function (PyInit_nimkovgen) #86

Closed spookware closed 1 year ago

spookware commented 1 year ago

I am trying to import my nim file to python. Recieving this output/error:


  File "C:\Users\phili\AppData\Local\Programs\Python\Python311\Lib\site-packages\nimporter.py", line 979, in __validate_spec
    util.module_from_spec(spec)
  File "<frozen importlib._bootstrap>", line 573, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 1233, in create_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
ImportError: dynamic module does not define module export function (PyInit_nimkovgen)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\phili\source\repos\TOB Bot\TOB Bot\TOB_Bot.py", line 20, in <module>
    import nimporter, nimkovgen
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1140, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1080, in _find_spec
  File "C:\Users\phili\AppData\Local\Programs\Python\Python311\Lib\site-packages\nimporter.py", line 1269, in find_spec
    return Nimporter.import_nim_code(fullname, path, library=False)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\phili\AppData\Local\Programs\Python\Python311\Lib\site-packages\nimporter.py", line 957, in import_nim_code
    cls.__validate_spec(spec)
  File "C:\Users\phili\AppData\Local\Programs\Python\Python311\Lib\site-packages\nimporter.py", line 1025, in __validate_spec
    raise NimporterException(error_message) from import_error
nimporter.NimporterException: Error importing C:\Users\phili\source\repos\TOB Bot\TOB Bot\__pycache__\nimkovgen.pyd
Error Message:

    dynamic module does not define module export function (PyInit_nimkovgen)

Python Version:

    3.11.1 (tags/v3.11.1:a7a450f, Dec  6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)]

Nim Version:

    Nim Compiler Version 1.6.10 [Windows: amd64]

CC Version:

    vccUndefined: C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat

Installed CCs:

    {'msc': WindowsPath('C:/Users/phili/Downloads/nim-1.6.10_x64/nim-1.6.10/bin/vccexe.EXE'), 'gcc': WindowsPath('C:/Users/phili/Downloads/nim-1.6.10_x64/nim-1.6.10/dist/mingw64/bin/gcc.EXE')}

Please help improve Nimporter by opening a bug report at: https://github.com/Pebaz/nimporter/issues/new and submit the above information along with your description of the issue.

Press any key to continue . . .```

Please keep in mind I am not a very experienced coder, and simply want to use this nim code in my python package. If you need anything from my end, try and explain it in a simple manner :)
Pebaz commented 1 year ago

Hi @spookware can you post the Nim file? I think you might be missing exportpy. Make sure to compare your code with the README since Nim code has to be annotated in order to be used by Python.

spookware commented 1 year ago

This is the code in the nim file:


import nimkov/[generator, objects, validators]
import nimpy

proc generate(file: File): auto =
  var file = open("messages.txt")
  let phrases = file.readAll().split("\n")
  file.close()

  let markov = newMarkov(phrases)

  return markov.generate().get```
Pebaz commented 1 year ago

All functions you want to have exposed to Python need: {.exportpy.}

That should fix this error for you. Let me know if it doesn’t work.