I try to create a DLL out of my Python program. Therefore I used Cython to convert the Python program into a C program and simply followed the steps mentioned in the answer for this question to call my methods. However, this works fine for simple methods where no additional libraries are required but not when I want to use my Logistic Regression.
I created beforehand a json file out of my trained ML algorithm, which I afterwards loaded in my predict.pyx file with sklearn_json. In my predict.pyx file are the following methods included:
import sklearn_json as skljson
# predict for the incoming values
cdef public int predict(double val_one, double val_two):
deserialized_model = skljson.from_json('json_model')
prediction = deserialized_model.predict([[val_one, val_two]])
return int(prediction[0])
# say hello and return an integer
cdef public int sayHello():
print("Hello, I am your python program.\n")
cdef int a = 5
return a
Afterwards I created through the command line the C files with:
Cython predict.pyx
Then I built two classes similar to cyfun_dll.c and cyfun_dll.h from the answer of this post.
and I ran the following commands in the command line:
gcc -c -DBUILD_DLL predict.c -IC:/Users/Hannah/Anaconda3/include
gcc -c -DBUILD_DLL predict_dll.c -IC:/Users/Hannah/Anaconda3/include
gcc -shared -o hello.dll predict.o predict_dll.o -LC:/Users/Hannah/Anaconda3/libs -lpython38
to create my DLL.
Furthermore, I also have a dllhandler.c to call the methods from my created DLL. However, as soon as I execute dllhandler.exe I got the following error:
Traceback (most recent call last):
File "predict.pyx", line 4, in init predict
import sklearn_json as skljson
ModuleNotFoundError: No module named 'sklearn_json'
Error occurred when loading library: 1114
I could solve this error through copying the folder "sklearn_json" from the site-packages from Anaconda into the folder from my program. However, afterwards I got the following error:
File "predict.pyx", line 4, in init predict
import sklearn_json as skljson
File "C:\Users\Hannah\Documents\VS Code - Cython V3\sklearn_json\__init__.py", line 1, in <module>
from sklearn_json import classification as clf
File "C:\Users\Hannah\Documents\VS Code - Cython V3\sklearn_json\classification.py", line 1, in <module>
import numpy as np
ModuleNotFoundError: No module named 'numpy'
Error occurred when loading library: 1114
This problem could be solved through copying the numpy directory from the Cython folder into the directory of my program. However, the same problem arrose with scipy, but this couldn't be solved through copying the scipy folder. I got the following error:
Traceback (most recent call last):
File "predict.pyx", line 4, in init predict
import sklearn_json as skljson
File "C:\Users\Hannah\Documents\VS Code - Cython V3\sklearn_json\__init__.py", line 1, in <module>
from sklearn_json import classification as clf
File "C:\Users\Hannah\Documents\VS Code - Cython V3\sklearn_json\classification.py", line 2, in <module>
import scipy as sp
File "C:\Users\Hannah\Documents\VS Code - Cython V3\scipy\__init__.py", line 61, in <module>
from numpy import show_config as show_numpy_config
ImportError: cannot import name 'show_config' from 'numpy' (unknown location)
I don't think that it should be necessary to change the code in the files. Additionally, I think that there must be a much better solution than copying the folders.
Does anybody have experience with Cython and additional libraries? How could I solve this error? Is there a better way to solve my problems without copying the folders? Or is there a way to use a ML model in json format to call and use for predictions in C directly. Thanks for any addvice.
EDIT: As suggested in the comments I looked at my sys.path which looks the following:
['C:\\Users\\Hannah\\Documents\\VS Code - Cython V3', 'C:\\Users\\Hannah\\Anaconda3\\python38.zip', 'C:\\Users\\Hannah\\Anaconda3\\DLLs', 'C:\\Users\\Hannah\\Anaconda3\\lib', 'C:\\Users\\Hannah\\Anaconda3', 'C:\\Users\\Hannah\\Anaconda3\\lib\\site-packages', 'C:\\Users\\Hannah\\Anaconda3\\lib\\site-packages\\win32', 'C:\\Users\\Hannah\\Anaconda3\\lib\\site-packages\\win32\\lib', 'C:\\Users\\Hannah\\Anaconda3\\lib\\site-packages\\Pythonwin']
This means my program should know where to find the packages like numpy, sklearn_json etc as it knows the path to the site-packages - but it doesn't work.
Maybe it is also good to know (as I saw it in the linked post in the comment) that I added python38.dll and python38.zip also in the same folder as my programs to make the python file callable from my DLL.
Second Edit: Is there any possibility, that the problem lies in the settings.json file in Visual Studio Code? Maybe there the path points to the wrong directory and it is therefore required to copy all required libraries? I do not know which settings should be changed there, has anybody a suggestion?