Microsoft.Scripting.SyntaxErrorException: ‘invalid syntax’

  Uncategorised

I’m trying to run a python with C#. The purpose of the python script is just an os.walk in a directory. I’m currently using IronPython. But when I’m trying to run the c# program I’m getting this exception: enter image description hereMicrosoft.Scripting.SyntaxErrorException: ‘invalid syntax’

I went to the python script and saw no syntax error. When I change my python script source code to
a simple print("hello world"), it works perfectly.
Why I’m getting this error?
Here is my C# code:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IronPython;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;


namespace TestIronPython
{
class Program
{
    static void Main(string[] args)
    {
        
        var script = @"D:Github RPythonCacheTestironpythonvenvmainmain.py";
        ScriptEngine engine = Python.CreateEngine();
        engine.ExecuteFile(script);
        Console.ReadLine();
    }
   

}
}

And here is my python code:

import sys
import os
from pathlib import Path

Drive_Path = Path(f"D:Github")
for x in os.walk(Drive_Path):
print(x)
print('done')

Source: Visual Studio Questions

One Reply to “Microsoft.Scripting.SyntaxErrorException: ‘invalid syntax’”

  • I am to getting the same error. I am trying to import pytesseract.

    I have added the library in searchPaths as well.
    searchPaths.Add(@”C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\Lib\site-packages\pytesseract”);

    Something is wrong with the library location, if I run the .py script independent then it works.
    Not sure what’s wrong.

LEAVE A COMMENT