【发布时间】:2018-07-31 11:58:12
【问题描述】:
当源 Python 脚本在子目录中有模块时,我正在努力解决在 Linux 中从 Python 文件创建 IronPython DLL 不起作用的问题。与 Windows 完全相同的构建工作正常。
我创建了一个简化的示例来演示这一点,can be found here。
该示例使用 Docker 快速启动 Linux 容器以在 Linux 中构建 IronPython DLL。然后我通过 LINQPad(参见RunCompiledRoutine.linq)运行了一些 C#,它试图运行生成的 IronPython DLL。这是失败的地方(当 IronPython DLL 是在 Linux 中构建时)。
要运行示例,只需运行go.ps1 脚本。
抱歉,我知道要运行该示例,它需要安装 Docker 和 LINQPad,所以我还将在下面解释它...
我正在尝试构建的主要源 Python 脚本是 TestScript.py 文件,它只包含这个...
import sys
import os
from TestSubDirectory.TestSubModule import TestSubModule
def main():
TestSubModule().PrintMessage()
所以,没什么特别的 - 它只是在名为 TestSubDirectory 的目录中导入一个模块,而该模块看起来像这样......
import sys
import os
class TestSubModule():
def PrintMessage(self):
print "*** SUCCESS ***"
所以实际上只是在屏幕上打印一条消息。
然后在Linux容器中,我只是为了构建编译好的IronPython DLL...
mono ./IronPython/ipy.exe ./CompileIntoDll.py
CompileIntoDll.py 只是在 clr.CompileModules("/app/CompiledPython.dll", mainModule = None, *files) 中包含要包含的文件列表。
这成功地构建了 IronPython DLL。但是,当我尝试从 C# 运行它时(请参阅RunCompiledRoutine.linq),它失败了...
ImportException: cannot import TestSubModule from TestSubDirectory
at IronPython.Runtime.Importer.ImportNestedModule(CodeContext context, PythonModule module, String[] parts, Int32 current, List path)
at IronPython.Runtime.Importer.ImportModuleFrom(CodeContext context, Object from, String[] parts, Int32 current)
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 DLRCachedCode.TestScript$1(CodeContext $globalContext, FunctionCode $functionCode)
at IronPython.Compiler.OnDiskScriptCode.Run()
at IronPython.Compiler.OnDiskScriptCode.Run(Scope scope)
at IronPython.Runtime.PythonContext.InitializeModule(String fileName, ModuleContext moduleContext, ScriptCode scriptCode, ModuleOptions options)
at IronPython.Runtime.ModuleLoader.load_module(CodeContext context, String fullName)
at Microsoft.Scripting.Interpreter.FuncCallInstruction`4.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3)
at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
at IronPython.Runtime.PythonContext.Call(CodeContext context, Object func, Object arg0)
at IronPython.Runtime.Importer.FindAndLoadModuleFromImporter(CodeContext context, Object importer, String fullName, List path, Object& ret)
at IronPython.Runtime.Importer.TryLoadMetaPathModule(CodeContext context, String fullName, List path, Object& ret)
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.Hosting.PythonService.ImportModule(ScriptEngine engine, String name)
at IronPython.Hosting.Python.ImportModule(ScriptEngine engine, String moduleName)
at UserQuery.Main(String[] args) in C:\Code\IronPythonLinuxIssue\RunCompiledRoutine.linq:line 12
at LINQPad.ExecutionModel.ClrQueryRunner.Run()
at LINQPad.ExecutionModel.Server.RunQuery(QueryRunner runner)
请注意,在 Windows 中构建 DLL 并在同一 LINQPad 脚本中运行可以正常工作。我想知道从 Linux 运行时 IronPython 是否没有正确处理路径分隔符(斜杠)。
使用 Linux 构建也可以正常工作如果我将 TestSubModule.py 与基本 TestScript.py 放在同一文件夹中(即不使用子目录)。
请注意,这是使用 IronPython 2.7.8 的 .NET 4.5 版本。我尝试使用 .NET Core 2.0 版本,但无法说明不支持 clr.CompileModules。
【问题讨论】:
标签: ironpython