Hi - I've got a single starting problem:
nimporter.NimCompileException: Error: unhandled exception: The system cannot find the file specified.
The source is the tutorial from statch, which suggests takes 3 steps to compare python and nim, but as you see I have run into this error:
nimporter.NimCompileException: Error: unhandled exception: The system cannot find the file specified.
The tutorial creates these 3 files in the same folder:
main.py
import nimporter
from time import perf_counter
import nmath # Nim imports!
import pmath
print("Measuring python...")
start_py = perf_counter()
for i in range(0,40):
print(pmath.fib(i))
end_py = perf_counter()
print("Measuring Nim...")
start_nim = perf_counter()
for i in range(0,40):
print(nmath.fib(i))
end_nim = perf_counter()
print("--------------")
print("Python elapsed", end_py-start_py)
print("Nim elapsed", end_nim-start_nim)
pmath.py
def fib(n):
if n==0:
return 0
elif n<3:
return 1
return fib(n-1) + fib(n-2)
nmath.nim
import nimpy
proc fib(n: int): int {.exportpy.} =
if n==0:
return 0
elif n<3:
return 1
return fib(n-1) + fib(n-1)
When running main.py I get the following error:
(pages310) D:\github\nim_test>C:/Data/venv/pages310/Scripts/python.exe d:/github/nim_test/main.py
Traceback (most recent call last):
File "d:\github\nim_test\main.py", line 5, in <module>
import nmath # Nim imports!
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1002, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 945, in _find_spec
File "C:\Data\venv\pages310\lib\site-packages\nimporter.py", line 1269, in find_spec
return Nimporter.import_nim_code(fullname, path, library=False)
File "C:\Data\venv\pages310\lib\site-packages\nimporter.py", line 944, in import_nim_code
NimCompiler.compile_nim_code(
File "C:\Data\venv\pages310\lib\site-packages\nimporter.py", line 705, in compile_nim_code
cls.check_compile_errors(errors, library, nim_args, output, tmp_cwd, warnings)
File "C:\Data\venv\pages310\lib\site-packages\nimporter.py", line 492, in check_compile_errors
raise NimCompileException(errors[0])
nimporter.NimCompileException: Error: unhandled exception: The system cannot find the file specified.
Things that work:
D:\github\nim_test>nim c -r "d:\github\nim_test\nmath.nim"
Hint: used config file 'C:\Users\madsenbj\Downloads\nim-1.6.8_x64\nim-1.6.8\config\nim.cfg' [Conf]
Hint: used config file 'C:\Users\madsenbj\Downloads\nim-1.6.8_x64\nim-1.6.8\config\config.nims' [Conf]
........................................................................................................
C:\Users\madsenbj\.nimble\pkgs\nimpy-0.2.0\nimpy.nim(761, 25) Hint: 'fibPy_wrapper' should be: 'fibPyWrapper' [Name]
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/system/ansi_c.nim
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/std/private/digitsutils.nim
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/system/assertions.nim
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/system/dollars.nim
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/system/io.nim
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/system.nim
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/pure/strutils.nim
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/pure/dynlib.nim
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/windows/winlean.nim
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/pure/times.nim
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/std/private/win_setenv.nim
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/pure/os.nim
CC: C:/Users/madsenbj/.nimble/pkgs/nimpy-0.2.0/nimpy/py_types.nim
CC: C:/Users/madsenbj/.nimble/pkgs/nimpy-0.2.0/nimpy/py_lib.nim
CC: C:/Users/madsenbj/.nimble/pkgs/nimpy-0.2.0/nimpy/py_utils.nim
CC: C:/Users/madsenbj/.nimble/pkgs/nimpy-0.2.0/nimpy/py_nim_marshalling.nim
CC: C:/Users/madsenbj/.nimble/pkgs/nimpy-0.2.0/nimpy.nim
CC: nmath.nim
Hint: [Link]
Hint: gc: refc; opt: none (DEBUG BUILD, `-d:release` generates faster code)
59578 lines; 2.133s; 93.648MiB peakmem; proj: d:\github\nim_test\nmath.nim; out: d:\github\nim_test\nmath.exe [SuccessX]
Hint: d:\github\nim_test\nmath.exe [Exec]
pip freeze reveals nimporter==1.1.0
I'm on python 3.10.8
and
>nim shows:
D:\github\nim_test>nim
Nim Compiler Version 1.6.8 [Windows: amd64]
Compiled at 2022-09-27
What could I be missing?
Kind regards Bjorn
Thank you for the quick response @Araq.
I chose the tutorial because of its relevance for other tasks I need to solve from python.
I have found the answer on nimpy under the headline of:
"implementing a Python Module in Nim" hinted subtly as:
# Compile on Windows:
nim c --app:lib --out:mymodule.pyd --threads:on --tlsEmulation:off --passL:-static mymodule
# Compile on everything else:
nim c --app:lib --out:mymodule.so --threads:on mymodule
So the error was that nmath.pyd couldn't be found as I had not compiled the python binary.
By running:
nim c --app:lib --out:nmath.pyd --threads:on --tlsEmulation:off --passL:-static nmath
The nmath.pyd file was created with:
(pages310) D:\github\nim_test>nim c --app:lib --out:nmath.pyd --threads:on --tlsEmulation:off --passL:-static nmath
Hint: used config file 'C:\Users\madsenbj\Downloads\nim-1.6.8_x64\nim-1.6.8\config\nim.cfg' [Conf]
Hint: used config file 'C:\Users\madsenbj\Downloads\nim-1.6.8_x64\nim-1.6.8\config\config.nims' [Conf]
................................................................................................................
C:\Users\madsenbj\.nimble\pkgs\nimpy-0.2.0\nimpy.nim(761, 25) Hint: 'fibPy_wrapper' should be: 'fibPyWrapper' [Name]
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/system/ansi_c.nim
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/std/private/digitsutils.nim
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/system/assertions.nim
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/system/dollars.nim
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/pure/collections/sharedlist.nim
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/system/io.nim
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/system.nim
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/pure/strutils.nim
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/pure/dynlib.nim
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/windows/winlean.nim
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/pure/times.nim
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/std/private/win_setenv.nim
CC: C:/Users/madsenbj/Downloads/nim-1.6.8_x64/nim-1.6.8/lib/pure/os.nim
CC: C:/Users/madsenbj/.nimble/pkgs/nimpy-0.2.0/nimpy/py_types.nim
CC: C:/Users/madsenbj/.nimble/pkgs/nimpy-0.2.0/nimpy/py_lib.nim
CC: C:/Users/madsenbj/.nimble/pkgs/nimpy-0.2.0/nimpy/py_utils.nim
CC: C:/Users/madsenbj/.nimble/pkgs/nimpy-0.2.0/nimpy/py_nim_marshalling.nim
CC: C:/Users/madsenbj/.nimble/pkgs/nimpy-0.2.0/nimpy.nim
CC: nmath.nim
Hint: [Link]
Hint: gc: refc; threads: on; opt: none (DEBUG BUILD, `-d:release` generates faster code)
61670 lines; 4.686s; 93.723MiB peakmem; proj: nmath; out: D:\github\nim_test\nmath.pyd [SuccessX]
I'm not sure (yet) on how to read the compiler instructions, but I will get there eventually.
At least my program runs, and I can only thank the nim-team that I've achieved more progress in 2 days with nim that in a month with Julia.