As a premise. A development project for Nim was launched, but since there was a requirement to handle statistical processing easily, there was a demand for casual use of python, so there was a technical requirement that nimpy could not manage. However, it was a lot of pain, so I summarized it. I hope it helps those who have struggled. It is an article called.
Native language integration with Python has never been easier!
A library that provides an interface that allows you to call python from nim or call nim from python.
I found many cases of calling nim from python, but I didn't have an example of calling python from nim, so I tried it. Or rather, I needed it, so I tried it.
Apparently you can call it with pyImport
.
pyImport
nimpy can use the python module installed by default by nimble install nimpy
as follows. (For some reason, numpy is included as a standard module on mac)
pyImport("numpy")
However, even if you want to use a library such as sklern, you cannot use it because it uses the OS standard python. In such a case, there is no choice but to put it in the python PATH. There is a method for that.
import nimpy
import nimpy/py_lib as pyLib
pyLib.pyInitLibPath("python path")
discard pyBuiltinsModule()
You can specify the python to call by calling with this syntax. But this requires you to specify the lipython Path.
If you read the nimpy code, it seems that you need to specify the python generated by dylib
to read it here.
In the environment installed by pyenv, there was no libpython generated in the form of libpython ** m.dylib
.
First of all, I needed to generate it, so execute the following.
CONFIGURE_OPTS="--enable-shared" pyenv install {version}
Then a file like libpython ** m.dylib
will be generated, so set the path to pyLib.pyInitLibPath
If you specify with, you can use the requested Python module behavior and the module installed with pip.
If you run it in a Docker environment etc., it may have been launched without delay, but since it got stuck with local run, I hope it will be a knowledge for that!
I thought Nim was good.
Recommended Posts