我使用EdgeJS和IronPython在我的NodeJS服务器中运行Python脚本,但IronPython无法导入模块。
我收到此错误:
System.Reflection.TargetInvocationException: Exception has been thrown by the ta
rget of an invocation. ---> IronPython.Runtime.Exceptions.ImportException: No mo
dule named __future__
at Microsoft.Scripting.Runtime.LightExceptions.CheckAndThrow(Object value)
at Microsoft.Scripting.Interpreter.FuncCallInstruction`2.Run(InterpretedFrame
frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0)
at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)
at IronPython.Runtime.PythonContext.InitializeModule(String fileName, ModuleC
ontext moduleContext, ScriptCode scriptCode, ModuleOptions options)
at IronPython.Runtime.PythonContext.CompileModule(String fileName, String mod
uleName, SourceUnit sourceCode, ModuleOptions options, ScriptCode& scriptCode)
at IronPython.Runtime.Importer.LoadModuleFromSource(CodeContext context, Stri
ng 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 nam
e, String fullName, List path)
at IronPython.Runtime.Importer.ImportTopAbsolute(CodeContext context, String
name)
at IronPython.Runtime.Importer.ImportModule(CodeContext context, Object globa
ls, String modName, Boolean bottom, Int32 level)
at IronPython.Modules.Builtin.__import__(CodeContext context, String name, Ob
ject 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 f
ullName, PythonTuple from, Int32 level)
at IronPython.Runtime.Operations.PythonOps.ImportWithNames(CodeContext contex
t, 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.Run1[T0,TRet](T0 arg0)
at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)
at IronPython.Runtime.PythonContext.InitializeModule(String fileName, ModuleC
ontext moduleContext, ScriptCode scriptCode, ModuleOptions options)
at IronPython.Runtime.PythonContext.CompileModule(String fileName, String mod
uleName, SourceUnit sourceCode, ModuleOptions options, ScriptCode& scriptCode)
at IronPython.Runtime.Importer.LoadModuleFromSource(CodeContext context, Stri
ng 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 nam
e, String fullName, List path)
at IronPython.Runtime.Importer.ImportTopAbsolute(CodeContext context, String
name)
at IronPython.Runtime.Importer.ImportModule(CodeContext context, Object globa
ls, String modName, Boolean bottom, Int32 level)
at IronPython.Modules.Builtin.__import__(CodeContext context, String name, Ob
ject 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 System.Dynamic.UpdateDelegates.UpdateAndExecute6[T0,T1,T2,T3,T4,T5,TRet](C
allSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
at IronPython.Runtime.Importer.ImportLightThrow(CodeContext context, String f
ullName, PythonTuple from, Int32 level)
at IronPython.Runtime.Operations.PythonOps.ImportTop(CodeContext context, Str
ing fullName, Int32 level)
at Microsoft.Scripting.Interpreter.FuncCallInstruction`4.Run(InterpretedFrame
frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0)
at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
at IronPython.Compiler.RuntimeScriptCode.Run()
at Microsoft.Scripting.Hosting.ScriptSource.Execute()
at EdgeCompiler.CompileFunc(IDictionary`2 parameters)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments,
Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Objec
t[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invoke
Attr, Binder binder, Object[] parameters, CultureInfo culture)
at ClrFunc.Initialize(Handle<v8::Value>* , Arguments* args)
在我的目录中,我有:
-src
|
- metacritic.py
- BeautifulSoup.py
- py.js
- search.py
前两个文件来自the Metacritic scraper,而py.js
是调用我的search.py
Py.js
var edge = require('edge');
var search = edge.func('py', 'search.py');
search('Viva la Vida', function (error, result) {
if (error) throw error;
console.log(result);
});
Search.py
import metacritic
def search(obj):
return metacritic.mainSearch("Viva la Vida", "album");
lambda x: search(x)
有趣的是,如果我在同一个文件夹中创建一个名为test.py
的临时文件,其代码为:
import metacritic
data = metacritic.mainSearch("Viva la Vida", "album");
print(data)
有效。但IronPython在这个导入上磕磕绊绊。
任何解决方案?
答案 0 :(得分:0)
看起来IronPython无法找到其标准库。您可能需要调整py.js并添加对ScriptEngine.SetSearchPaths
的调用以包含标准库位置。