Pebaz / nimporter

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

[Bug] dynamic module does not define module export function #53

Closed knassar702 closed 2 years ago

knassar702 commented 2 years ago

hello , im trying to run simple nim code inside python script with nimporter but i always got an error about importing error from GCC

proc main() =
   echo "Hello World From NIm"
import nimporter
import nscript

print("Word :D")
☰ Error ``` $ python test.py gcc: fatal error: no input files compilation terminated. Traceback (most recent call last): File "/home/knassar702/.local/lib/python3.9/site-packages/nimporter.py", line 863, in __validate_spec util.module_from_spec(spec) File "", line 565, in module_from_spec File "", line 1173, in create_module File "", line 228, in _call_with_frames_removed ImportError: dynamic module does not define module export function (PyInit_nscript) The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/home/knassar702/nim_test/test.py", line 2, in import nscript File "", line 1007, in _find_and_load File "", line 982, in _find_and_load_unlocked File "", line 925, in _find_spec File "/home/knassar702/.local/lib/python3.9/site-packages/nimporter.py", line 1150, in find_spec return Nimporter.import_nim_code(fullname, path, library=False) File "/home/knassar702/.local/lib/python3.9/site-packages/nimporter.py", line 841, in import_nim_code cls.__validate_spec(spec) File "/home/knassar702/.local/lib/python3.9/site-packages/nimporter.py", line 909, in __validate_spec raise NimporterException(error_message) from import_error nimporter.NimporterException: Error importing /home/knassar702/nim_test/__pycache__/nscript.so Error Message: dynamic module does not define module export function (PyInit_nscript) Python Version: 3.9.6 (default, Jun 30 2021, 15:31:20) [GCC 11.1.0] Nim Version: Nim Compiler Version 1.4.8 [Linux: amd64] CC Version: Installed CCs: {'gcc': PosixPath('/usr/bin/gcc')} 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. ```

Env

Pebaz commented 2 years ago

You gotta use the exportpy:

# mymodule.nim - file name should match the module name you're going to import from python
import nimpy

proc greet(name: string): string {.exportpy.} =
  return "Hello, " & name & "!"

Lookup the docs for Nimpy as Nimporter just makes Nimpy modules easier to import. Also, main functions are not supported I don't think since the Nim code is supposed to be imported, not run directly.

knassar702 commented 2 years ago

thanks for your answer , its work :D