I would like to import the numpy
Python library into Nim's code. The following code is my attempt to do so:
import os
import nimpy
let pythonPath = "C:/User/Master/anaconda3"
putEnv("PYTHONPATH", pythonPath)
let pythonHome = "C:/User/Master/anaconda3/Lib/site-packages"
putEnv("PYTHONHOME", pythonHome)
let np = pyImport("numpy")
let arr = np.array([1, 2, 3, 4])
echo "Import numpy is success!"
echo arr
This code errors into the following error.
C:\MyPrograms\Nim_Programs\Training_Project>C:/Users/Master/anaconda3/Scripts/activate
(base) C:\MyPrograms\Nim_Programs\Training_Project>conda activate base
(base) C:\MyPrograms\Nim_Programs\Training_Project>C:/Users/Master/anaconda3/python.exe c:/MyPrograms/Nim_Programs/Training_Project/CompairPythonAndNim/searchPythonFile.py
C:\Users\Master\anaconda3\lib\site-packages\numpy\__init__.py
(base) C:\MyPrograms\Nim_Programs\Training_Project>nim c -r ImportPython.nim
Hint: used config file 'C:\Users\Master\.choosenim\toolchains\nim-1.6.12\config\nim.cfg' [Conf]
Hint: used config file 'C:\Users\Master\.choosenim\toolchains\nim-1.6.12\config\config.nims' [Conf]
.........................................................................................................
CC: ../../../Users/Master/.choosenim/toolchains/nim-1.6.12/lib/system.nim
CC: ImportPython.nim
Hint: [Link]
Hint: gc: refc; opt: none (DEBUG BUILD, `-d:release` generates faster code)
59660 lines; 2.194s; 93.867MiB peakmem; proj: C:\MyPrograms\Nim_Programs\Training_Project\ImportPython.nim; out: C:\MyPrograms\Nim_Programs\Training_Project\ImportPython.exe [SuccessX]
Hint: C:\MyPrograms\Nim_Programs\Training_Project\ImportPython.exe [Exec]
Python path configuration:
PYTHONHOME = 'C:/User/Master/anaconda3/Lib/site-packages'
PYTHONPATH = 'C:/User/Master/anaconda3'
program name = 'python'
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = 'C:\\MyPrograms\\Nim_Programs\\Training_Project\\ImportPython.exe'
sys.base_prefix = 'C:/User/Master/anaconda3/Lib/site-packages'
sys.base_exec_prefix = 'C:/User/Master/anaconda3/Lib/site-packages'
sys.platlibdir = 'lib'
sys.executable = 'C:\\MyPrograms\\Nim_Programs\\Training_Project\\ImportPython.exe'
sys.prefix = 'C:/User/Master/anaconda3/Lib/site-packages'
sys.exec_prefix = 'C:/User/Master/anaconda3/Lib/site-packages'
sys.path = [
'C:/User/Master/anaconda3',
'C:\\Users\\Master\\anaconda3\\python310.zip',
'C:/User/Master/anaconda3/Lib/site-packages\\DLLs',
'C:/User/Master/anaconda3/Lib/site-packages\\lib',
'C:\\MyPrograms\\Nim_Programs\\Training_Project',
]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00000b54 (most recent call first):
<no Python frame>
Error: execution of an external program failed: 'C:\MyPrograms\Nim_Programs\Training_Project\ImportPython.exe '
I have installed Nim and anaconda3. In my environment, I can use pip
, conda
, nim
, nimble
commands in shell. In addition to these, I have already imported nimpy.
What should I do to import numpy
into Nim's code?