6

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?

denfromufa
  • 5,610
  • 13
  • 81
  • 138

1 Answers1

4

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
>
Evk
  • 98,527
  • 8
  • 141
  • 191
  • The point is interactively testing CPython numerical code embedded in C#. Ironpython is not an option. – denfromufa Sep 19 '15 at 00:40
  • I need to test under .NET, because that is production environment, not Mono. – denfromufa Sep 19 '15 at 00:41
  • .NET is production environment but in scripts you loading under mono in your example. So you are working under mono, and Pythonnet won't work there without recompilation under mono. – Evk Sep 19 '15 at 09:00
  • Also look at http://code.google.com/p/ironclad/. If you give concrete example of what are you trying to do, and why you need scriptcs for that - maybe I can help you more – Evk Sep 19 '15 at 09:04
  • Mono is used for introspection in scriptcs. Ironclad and other related projects are stalled for Ironpython since ~2012. Have you even tried using scipy with Ironpython?! – denfromufa Sep 19 '15 at 14:00
  • I see IronPython is not an option for you. Well, you asked how to load pythonnet runtime dll under mono environment in scriptcs, I answered - try to compile it with mono compiler from source. – Evk Sep 19 '15 at 14:07