Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for scriptcs `dynamic` is not supported by Roslyn and available only with mono, but mono fails on DLL

I'm try to load pythonnet runtime dll from scriptcs and it does not work with roslyn backend because dynamic is not supported in Roslyn, but mono backend chokes with the following error:

$ 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

Question:


How can I get Mono backend of scriptcs working with Python.Runtime.DLL? Do I need to load any DLLs before that? Does the Python.Runtime.DLL has to be compiled with Mono support or .NET is fine?

like image 862
denfromufa Avatar asked Sep 09 '15 05:09

denfromufa


1 Answers

This error (CS0009 with mono compiler) is quite generic and means "something wrong" with given assembly, so quite hard to debug. One way to proceed is indeed compile under mono. If you download their source code (https://github.com/pythonnet) you will find instructions on how to do that (look at pythonnet\src\monoclr\README.txt also).

However, depending on your goals, you can consider using IronPython instead, which is officially supported by mono and runs out of the box. Example below assumes you downloaded zip of IronPython and extracted it somewhere:

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
>
like image 62
Evk Avatar answered Oct 20 '22 00:10

Evk