Microsoft.Scripting.SyntaxErrorException:'使用IronPython导入matplotlib时出现意外令牌'='

时间:2018-12-04 13:51:17

标签: c# python matplotlib ironpython

我找不到这种奇怪现象的答案,我真的希望有人能帮忙...

我正在使用IronPython 2.7.9从.NET 4.5 C#代码运行python代码(在Visual Studio 2017 Proffesional上),并得到以下异常:

Microsoft.Scripting.SyntaxErrorException: 'unexpected token '=''

在python代码中从matplotlib导入的行上。

这是我的C#代码:

ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
var searchPaths = engine.GetSearchPaths();
searchPaths.Add(@"C:\Users\liron\Documents\Visual Studio 2017\Projects\B3D_Python\packages\IronPython.2.7.9\lib");
engine.SetSearchPaths(searchPaths);
engine.ExecuteFile(@"C:\Users\liron\Documents\Visual Studio 2017\Projects\B3D_Python\B3D_Python\B3D_Plotting.py", scope);
dynamic test1 = scope.GetVariable("run_animation");
test1();

和我的Python代码:

from datetime import datetime
from matplotlib import pyplot as plt
import matplotlib.animation as animation
import random

def run_animation():
    ...do something...

在第二行python中抛出异常(来自matplotlib import ...)。

从Visual Studio IronPython 2.7解释器运行时,python代码运行良好。

这是堆栈跟踪:

Microsoft.Scripting.SyntaxErrorException
HResult=0x80131500
Message=unexpected token '='
Source=IronPython
StackTrace:
at IronPython.Runtime.ThrowingErrorSink.Add(SourceUnit sourceUnit, String message, SourceSpan span, Int32 errorCode, Severity severity)
at IronPython.Compiler.Parser.ReportSyntaxError(Int32 start, Int32 end, String message, Int32 errorCode)
at IronPython.Compiler.Parser.ReportSyntaxError(Token t, IndexSpan span, Int32 errorCode, Boolean allowIncomplete)
at IronPython.Compiler.Parser.ReportSyntaxError(TokenWithSpan t, Int32 errorCode)
at IronPython.Compiler.Parser.ReportSyntaxError(TokenWithSpan t)
at IronPython.Compiler.Parser.Eat(TokenKind kind)
at IronPython.Compiler.Parser.FinishTupleOrGenExp()
at IronPython.Compiler.Parser.ParsePrimary()
at IronPython.Compiler.Parser.ParsePower()
at IronPython.Compiler.Parser.ParseFactor()
at IronPython.Compiler.Parser.ParseExpr(Int32 precedence)
at IronPython.Compiler.Parser.ParseExpr()
at IronPython.Compiler.Parser.ParseComparison()
at IronPython.Compiler.Parser.ParseNotTest()
at IronPython.Compiler.Parser.ParseAndTest()
at IronPython.Compiler.Parser.ParseOrTest()
at IronPython.Compiler.Parser.ParseExpression()
at IronPython.Compiler.Parser.ParseExpressionList(Boolean& trailingComma)
at IronPython.Compiler.Parser.ParsePrintStmt()
at IronPython.Compiler.Parser.ParseSmallStmt()
at IronPython.Compiler.Parser.ParseSimpleStmt()
at IronPython.Compiler.Parser.ParseStmt()
at IronPython.Compiler.Parser.ParseSuite()
at IronPython.Compiler.Parser.ParseIfStmtTest()
at IronPython.Compiler.Parser.ParseIfStmt()
at IronPython.Compiler.Parser.ParseStmt()
at IronPython.Compiler.Parser.ParseSuite()
at IronPython.Compiler.Parser.ParseClassOrFuncBody()
at IronPython.Compiler.Parser.ParseFuncDef()
at IronPython.Compiler.Parser.ParseDecorated()
at IronPython.Compiler.Parser.ParseStmt()
at IronPython.Compiler.Parser.ParseSuite()
at IronPython.Compiler.Parser.ParseClassOrFuncBody()
at IronPython.Compiler.Parser.ParseClassDef()
at IronPython.Compiler.Parser.ParseStmt()
at IronPython.Compiler.Parser.ParseFileWorker(Boolean makeModule, Boolean returnValue)
at IronPython.Compiler.Parser.ParseFile(Boolean makeModule, Boolean returnValue)
at IronPython.Runtime.PythonContext.ParseAndBindAst(CompilerContext context)
at IronPython.Runtime.PythonContext.CompilePythonCode(SourceUnit sourceUnit, CompilerOptions options, ErrorSink errorSink)
at IronPython.Runtime.PythonContext.GetScriptCode(SourceUnit sourceCode, String moduleName, ModuleOptions options, CompilationMode mode)
at IronPython.Runtime.PythonContext.GetScriptCode(SourceUnit sourceCode, String moduleName, ModuleOptions options)
at IronPython.Runtime.PythonContext.CompileModule(String fileName, String moduleName, SourceUnit sourceCode, ModuleOptions options, ScriptCode& scriptCode)
at IronPython.Runtime.PythonContext.CompileModule(String fileName, String moduleName, SourceUnit sourceCode, ModuleOptions options)
at IronPython.Runtime.Importer.LoadFromSourceUnit(CodeContext context, SourceUnit sourceCode, String name, String path)
at IronPython.Runtime.Importer.LoadModuleFromSource(CodeContext context, String name, String path)
at IronPython.Runtime.Importer.LoadPackageFromSource(CodeContext context, String name, String path)
at IronPython.Runtime.Importer.LoadFromDisk(CodeContext context, String name, String fullName, String str)
at IronPython.Runtime.Importer.ImportFromPathHook(CodeContext context, String name, String fullName, List path, Func`5 defaultLoader)
at IronPython.Runtime.Importer.ImportFromPath(CodeContext context, String name, String fullName, List path)
at IronPython.Runtime.Importer.ImportTopAbsolute(CodeContext context, String name)
at IronPython.Runtime.Importer.ImportModule(CodeContext context, Object globals, String modName, Boolean bottom, Int32 level)
at IronPython.Modules.Builtin.__import__(CodeContext context, String name, Object globals, Object locals, Object fromlist, Int32 level)
at Microsoft.Scripting.Interpreter.FuncCallInstruction`7.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run7[T0,T1,T2,T3,T4,T5,T6,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6)
at IronPython.Runtime.Importer.ImportLightThrow(CodeContext context, String fullName, PythonTuple from, Int32 level)
at IronPython.Runtime.Operations.PythonOps.ImportWithNames(CodeContext context, String fullName, String[] names, Int32 level)
at Microsoft.Scripting.Interpreter.FuncCallInstruction`5.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
at IronPython.Compiler.PythonScriptCode.Run(Scope scope)
at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)
at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink)
at Microsoft.Scripting.SourceUnit.Execute(Scope scope)
at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)
at Microsoft.Scripting.Hosting.ScriptEngine.ExecuteFile(String path, ScriptScope scope)
at PythonUsingApp.Class1.Main(String[] args) in C:\Users\liron\Documents\Visual Studio 2017\Projects\B3D_Python\PythonUsingApp\Class1.cs:line 10

请帮助!

2 个答案:

答案 0 :(得分:0)

根据2014年对这个问题的回答,不可能在IronPython中使用matplotlib:

import error of matplotlib with IronPython

尽管这是与我得到的错误不同的错误。

有人知道这是否仍然存在吗?是否可以在IronPython中使用matplotlib?

答案 1 :(得分:-2)

您可以尝试在C#附加的Lib文件夹中手动安装库,然后尝试。也许应该可行