0

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?

Cori
  • 39
  • 4
  • Which python installation is used by your embeded python? Do you provide it? Find out how `sys.path` of your embeded python looks like. Add path to site-packages-folder you what to use. For more background see for example: https://stackoverflow.com/q/56857449/5769463 – ead Jul 01 '21 at 09:26
  • Thank you for the advice @ead. I edited the post to add the information about sys.path. Regrettably it doesn't solve my problem. – Cori Jul 01 '21 at 12:22
  • Ok, that is strange. I have no idea why the packages in `C:\\Users\\Hannah\\Anaconda3\\lib\\site-packages` aren't seen. Do you have different Python versions on your machine that maybe something wrong goes picked up (maybe 32 vs 64 bit). But usually, you don't want to run embedded python with some arbitrary python installation but to provide this installation along with the embedded python in order to avoid surprises. – ead Jul 01 '21 at 12:37
  • Yes I do have different python versions but in this case I work with conda and also if I am in the terminal (in Visual Studio Code) and type "python --version" it says Python 3.8.5. Can the other versions still interfer? I am confused because I have not worked with locations of libraries till know because I never had troubles with it. Can I provide any further information which would help you? – Cori Jul 01 '21 at 13:37
  • Such problems are almost impossible to understand remotely, so I’m afraid I cannot help you there. First you must understand, why the standard site packages do not work (is numpy installed there at all? Is it correct version? Is it only numpy or any module from there) – ead Jul 01 '21 at 14:59
  • @ead I finally made my DLL work. Thanks to your advice with the sys.path - I printed the sys.path from my post in the wrong place. As I put my print of the sys.path in the right place, I saw that it was using the wrong python version. – Cori Jul 21 '21 at 05:54

0 Answers0