6

我正在尝试从 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

问题:


如何获得使用 Python.Runtime.DLL 的脚本的 Mono 后端?在此之前我需要加载任何 DLL 吗?Python.Runtime.DLL 是否必须使用 Mono 支持进行编译,或者 .NET 是否可以?

4

1 回答 1

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
>
于 2015-09-12T17:57:17.667 回答