对于scriptcs,"动态"不受Roslyn支持,只有单声道可用,但单声道在DLL上失败

den*_*ufa 6 c# mono python.net roslyn scriptcs

我尝试从scriptcs加载pythonnet运行时dll并且它不能与roslyn后端一起使用,因为Roslyn不支持动态,但单声道后端阻塞会出现以下错误:

$ scriptcs -modules mono
scriptcs (ctrl-c to exit or :help for help)

> #r "F:\Python\Python27\Lib\site-packages\Python.Runtime.dll"
error CS0009: Metadata file `F:\Python\Python27\Lib\site-packages\Python.Runtime.dll' does not contain valid metadata
Run Code Online (Sandbox Code Playgroud)

题:


如何使用Python.Runtime.DLL获取脚本的Mono后端?我需要在此之前加载任何DLL吗?是否必须使用Mono支持编译Python.Runtime.DLL或.NET是否正常?

Evk*_*Evk 4

这个错误(单声道编译器的 CS0009)非常普遍,意味着给定的程序集“出了问题”,因此很难调试。一种继续进行的方法确实是在单声道下编译。如果您下载他们的源代码(https://github.com/pythonnet),您将找到有关如何执行此操作的说明(另请参阅 pythonnet\src\monoclr\README.txt)。

但是,根据您的目标,您可以考虑使用 IronPython,它由 Mono 官方支持并且开箱即用。下面的示例假设您下载了 IronPython 的 zip 并将其解压到某处:

scriptcs -modules mono
scriptcs (ctrl-c to exit or :help for help)
> #r "G:\dev_tools\IronPython-2.7.5\IronPython-2.7.5\IronPython.dll"
> #r "G:\dev_tools\IronPython-2.7.5\IronPython-2.7.5\Microsoft.Dynamic.dll"
> #r "G:\dev_tools\IronPython-2.7.5\IronPython-2.7.5\Microsoft.Scripting.dll"
> using System;
> using System.IO;
> using IronPython.Hosting;
> using Microsoft.Scripting;
> using Microsoft.Scripting;
> using Microsoft.Scripting.Hosting;
> string script = "print 'hello world'";
> var engine = Python.CreateEngine();
> var scope = engine.CreateScope();
> var source = engine.CreateScriptSourceFromString(script, SourceCodeKind.Statements);
> var compiled = source.Compile();
> var result = compiled.Execute(scope);
hello world
>
Run Code Online (Sandbox Code Playgroud)