Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to execute a python script in my C# project The python script has some imported modules.
I use ironpython library in my c# project and set the python modules path for it but it does not work and gives me an invalid syntax error;However, there is no error in the Python script itself.
I also use pyinstaller to create an executable file from my python script for run it in my c# project but the output executable file also does not work and gives me the "Failed to execute script...." error.
Please help me....
Note that I want to be able to run my project on any computers without any problems.
Thanks.

What I have tried:

My python script codes:
Python
import speaker_verification_toolkit.tools as svt
import os
def MyFile(fileName):
    THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
    my_file = os.path.join(THIS_FOLDER, fileName)
    return my_file
def GetFiles():
    Files=[]
    basepath = MyFile('voices/')
    for entry in os.listdir(basepath):
     if os.path.isfile(os.path.join(basepath, entry)):
         Files.append(os.path.join(basepath, entry))
    return Files
def FilesMFCC():
    Signals=[]
    Files=GetFiles()
    for i in Files:
        s=svt.extract_mfcc_from_wav_file(MyFile(i),16000,0.025,0.01)
        Signals.append(svt.rms_silence_filter(s,16000,None,0.001135))
    return Signals
def SetResult(distance):
    if distance<70000:
        f= open(MyFile('verifylog.txt'),"w+")
        f.write("true")
        f.close()
    else:
        f= open(MyFile('verifylog.txt'),"w+")
        f.write("false")
        f.close()
x=svt.extract_mfcc_from_wav_file(MyFile('voicein.wav'),16000,0.025,0.01)
xx=svt.rms_silence_filter(x,16000,None,0.001135)
SigList=FilesMFCC()
result=svt.find_nearest_voice_data(FilesMFCC(),xx)
rr=svt.rms_silence_filter(SigList[result],16000,None,0.001135)
f1=svt.compute_distance(xx,rr)
SetResult(f1)


My C# Codes to run the python script:
C#
private bool RunSpeakVerifier()
        {
            var Engine = Python.CreateEngine();
            var Script = "SpeakerVerifier.py";
            var source = Engine.CreateScriptSourceFromFile(Script);
            ICollection<string> paths = Engine.GetSearchPaths();
            string dir = @"C:\Users\Mohammad Tavoosi\AppData\Local\Programs\Python\Python38\Lib\";
            paths.Add(dir);
            string dir2 = @"C:\Users\Mohammad Tavoosi\AppData\Local\Programs\Python\Python38\Lib\site-packages";
            paths.Add(dir2);
            Engine.SetSearchPaths(paths);
            source.Execute();
            if (File.Exists("verifylog.txt"))
            {
                string txt = File.ReadAllText("verifylog.txt");
                if (txt == "true") return true;
                else return false;
            }
            else return false;
        }
Posted
Updated 26-Jun-20 15:28pm
Comments
Richard MacCutchan 26-Jun-20 11:50am    
Where does the syntax error occur and what is the exact message?
Mohammad Tavoosi 26-Jun-20 12:22pm    
HiThe syntax error occur When I want to Run the python script in my C# project. at this line:
source.Execute();
and the exact message is:
SyntaxErrorException was unhandled
An unhandled exception of type 'Microsoft.Scripting.SyntaxErrorException' occurred in Microsoft.Dynamic.dll
Additional information: invalid syntax

1 solution

1) ok, you've verified that your Python Script works 'standalone' - tick, good

2) You haven't said what version of IronPython you're using - If I have it correct, the latest version of IronPython only implements Python 2.7

3) Following from 2, your Engine setup is adding Python 3.8 to the paths

4) So, I would ensure that the python script ONLY contains Python 2.7 compatible constructs, AND ensure only Python27 paths etc setup for the script

5) Failing (4), you could fire off a seperate process eg 'python27 SpeakerVerifier.py' (or even 'python38 SpeakerVerifier.py') but how you get the data to/from the process and the script will be a seperate issue to be solved
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900