Pebaz / nimporter

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

dynamic module does not define module export function (PyInit_commands) #62

Closed neroist closed 2 years ago

neroist commented 2 years ago

I'm trying to make a simple program using Python and Nim where the Python file calls a procedure that opens YouTube from the Nim file. And I get this error when importing the Nim file:

commands.nim:

import browsers

proc youtube* =
  openDefaultBrowser("https://www.youtube.com/")

main.py:

import nimporter
from commands import youtube

youtube()

And I get this error when importing the Nim file:

Error Message:

dynamic module does not define module export function (PyInit_commands)

Python Version:

3.10.1 (tags/v3.10.1:2cd268a, Dec  6 2021, 19:10:37) [MSC v.1929 64 bit (AMD64)]

Nim Version:

Nim Compiler Version 1.6.2 [Windows: amd64]

CC Version:

vccUndefined: C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvarsall.bat

Installed CCs:

{'msc': WindowsPath('C:/Nim/nim-1.6.2/bin/vccexe.EXE'), 'gcc': WindowsPath('C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gcc.EXE')}

Also, if I just call something simple like an echo, or anything else, it throws the same error! In addition, this might not be important but I installed pipwin before running this code and nimporter used to work before so i wonder if those two are connected.

Pebaz commented 2 years ago

Hello @nonimportant, thanks for submitting this!

If you add the the {.exportpy.} to the function signature, it will be properly exported during the Nim compilation step:

import browsers

proc youtube* {.exportpy.} =
    openDefaultBrowser("https://www.youtube.com/")

Nimporter builds upon Nimpy to expose Nim to the Python interpreter. There's a lot of useful unit tests that show the full capabilities of Nimpy!

I'll close this for now but please let me know if you encounter any other issues! 🙂