Call python from nim with Nimpy

Overview

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.

What is nimpy

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!

Summary

I thought Nim was good.

Recommended Posts

Call python from nim with Nimpy
Principal component analysis using python from nim with nimpy
Call CPLEX from Python (DO cplex)
Call the API with python3.
With skype, notify with skype from python!
Learn Nim with Python (from the beginning of the year).
Call Matlab from Python to optimize
Call a Python function from p5.js.
Using Rstan from Python with PypeR
Install Python from source with Ansible
Create folders from '01' to '12' with python
Call popcount from Ruby / Python / C #
Run Aprili from Python with Orange
Read fbx from python with cinema4d
Call C / C ++ from Python on Mac
Call c language from python (python.h)
Collecting information from Twitter with Python (Twitter API)
Receive textual data from mysql with python
Get html from element with Python selenium
[Note] Get data from PostgreSQL with Python
Play audio files from Python with interrupts
Create wordcloud from your tweet with python3
Tweet from python with Twitter Developer + Tweepy
Business efficiency starting from scratch with Python
Decrypt files encrypted with openssl from python with openssl
Working with Azure CosmosDB from Python Part.2
Image acquisition from camera with Python + OpenCV
Getting started with Dynamo from Python boto
Use C ++ functions from python with pybind11
Call APIGateWay with APIKey in python requests
Call a command from Python (Windows version)
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Integrate with Python
sql from python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Collecting information from Twitter with Python (Environment construction)
Csv output from Google search with [Python]! 【Easy】
Extract text from PowerPoint with Python! (Compatible with tables)
Wrap C with Cython for use from Python
~ Tips for Python beginners from Pythonista with love ① ~
Make OpenCV3 available from python3 installed with pyenv
Image processing from scratch with python (4) Contour extraction
Generate an insert statement from CSV with Python.
Call Python library for text normalization from MATLAB
Create a decision tree from 0 with Python (1. Overview)
Call a Python script from Embedded Python in C ++ / C ++
Install vim7.3 (+ python2.4) from source (compatible with Gundo.vim)
[Python] Dealing with multiple call errors in ray.init
Wrap C ++ with Cython for use from Python