意外的令牌附加

时间:2018-09-11 17:43:45

标签: c# python ironpython

我使用IronPython编写了一个简单的语言翻译器应用程序,并通过C#控制台应用程序调用了python代码。我分别测试了python代码,效果很好。但是,当我通过C#应用程序调用python代码时,它总是会给出 Microsoft.Scripting.SyntaxErrorException:'意外的令牌'append'错误消息。

ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromFile(@"D:\Projects\Translator.py");

ICollection<string> Paths = engine.GetSearchPaths();
Paths.Add(@"C:\Program Files\Python37\Lib\");
Paths.Add(@"C:\Program Files\Python37\Lib\site-packages\");
engine.SetSearchPaths(Paths);

ScriptScope scope = engine.CreateScope();
source.Execute(scope);

dynamic GoogleTranslator = scope.GetVariable("GoogleTranslator");
dynamic gTranslator = GoogleTranslator();
gTranslator.SetInfo("'Привет, корова мальчик'");
var result = gTranslator.Translate();

Console.Write(result);
Console.Read();

---------- Python代码-----------

from googletrans import Translator

class GoogleTranslator(object):
    Text = ""
    Dest  = ""

    def SetInfo(self, text, dest = 'en'):
        self.Text = text
        self.Dest = dest

    def Translate(self):
        translator = Translator()
        result = translator.translate(self.Text, self.Dest)
        return result.text

谁能告诉我代码的问题是什么。错误是由“ source.Execute(scope);”产生的。

0 个答案:

没有答案