Include Python in Electron App for ML


I have built a very simple machine learning model, and I’d like to include it in my electron app. (For context, the model scores short-response answers to math questions.) The model is built with scikit-learn, CountVectorizer, pandas, etc. (the usual). I’ve seen a lot of good information online about spawning child processes to run the script, but I assume that I need to package python and the requisite modules in the standalone app. How could one do this?


This isn’t the best place to ask Python-specific questions, but I have a little knowledge. There are a lot of examples of embedding Python, and the devs have provided official documentation on how to do it in C/C++. I imagine it would be possible to use that information to make a Node-compatible JavaScript library with native bindings for the Python runtime, but my search of the NPM registry didn’t turn anything up. The Python site lists WinPython and PyRun as natively portable (for Windows and Unix-based systems, respectively). If you packaged your app with a task runner (Grunt/Gulp/etc.) and had install instructions to download one of those based on the OS, that would probably do what you want.

Since the Python is a core part of your application, and considering the fact that many users of machine learning models will already have Python installed, you could expand on the idea of a task runner script to take a full survey of the local Python environment. If the user has all of the relevant packages and the right version of Python, you could give them a prompt asking if they want to download a new interpreter or use their own. If they want to use their own, you can have the task runner create a virtualenv folder (or even make that optional).

Another option is to use a Python framework that functions like Electron. I know of two, CEF Python (extensive feature set, very powerful, with a lot of high-end users which means that there should be a lot of robust examples out in the wild) and Eel (small, low overhead, perfect for prototyping).


Great answer. Thank you. Given how simple my model is, I’ll give brain.js a whirl and then return to these ideas if that doesn’t give me what I need.