2

I need to run python neural network script in witch using imports:

from keras.models import Sequential    
from keras.layers import Dense    
from keras.callbacks import History    
from keras.models import load_model    
import numpy as np    
import matplotlib.pyplot as plt    
np.random.seed(7)    
import pandas as pd    
from sklearn.model_selection import train_test_split    
from keras import optimizers

I try to run it using IronPython but this dont worked. IronPython dont recognise keras.models etc... I also try to run it with System.Diagnostic.Process but this solution also dont worked. It run only simple python scripts with some prints.

I need just to execute .py script from .cs class.

I try examples with cmd.exe with run .py script with but they dont worked.

Is there any way to run python neural network script in c# app? It must be very simple.

EDIT: One of my solutions: string fileName =

string fileName="C:/Users/FUJITSU/source/repos/CardiologicClinic_WebApp/CardiologicClinic_WebApp/AI/xd.py";
                Process p = new Process();
                p.StartInfo = new ProcessStartInfo(@"C:\Users\FUJITSU\PycharmProjects\NIDUC\venv\Scripts\python.exe", fileName);
                p.StartInfo.WorkingDirectory = "C:/Users/FUJITSU/source/repos/CardiologicClinic_WebApp/CardiologicClinic_WebApp/AI";
                p.Start();
                p.WaitForExit();
                p.Close();

And error produced:

line 1, in <module>
    from keras.models import load_model
ModuleNotFoundError: No module named 'keras'

2 Answers 2

1

Here is the solution:

 string fileName = "path to script.py";
            Process p = new Process();
            p.StartInfo = new ProcessStartInfo(@"path to python.exe", fileName);
            p.StartInfo.WorkingDirectory = "path to working dir";
            p.Start();
            p.WaitForExit();
            p.Close();

This worked fine for me.

Optionaly you can hide process with this command:

p.startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
Sign up to request clarification or add additional context in comments.

Comments

0

I don't think that every python library will work in IronPython. Posting any error messages you do have would be helpful in finding a solution.

Instead, try calling python using the C# version of system('python myscript.py'). It is sure to be tough going though getting two languages to work together.

(If I could comment, I would have made this a comment instead. Your forgiveness, please.)

3 Comments

thanks but this dont work for me -> error "no module named keras.model"
Yes. IronPython can not do what you want. You will have Python do what you want. Please see the medium article I linked in my previous comment. It shows you how to call a python process from c#. Good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.