IronPython

From IPRE Wiki
Jump to: navigation, search

The details on this page are now out-dated. Please see PyjamaDevelopment for current information.

IronPython

IronPython is a new version of Python written in C# to take advantage of the .NET framework. Mono is a cross-platform implementation of .NET. There is also a cross-platform version of IronPython called IronPython Community Edition (IPCE).

IronPython on Mono

To use IronPython 2.x you need at least Mono 1.9

You can see what version of Mono you have with:

mono --version

To install Mono (currently 1.9) with ipy included:

There wasn't a Fedora rpm, but there is a binary installer. It installs by default into /opt. /opt/mono-1.9/bin/ipy is a script to start IronPython.

Help

Build IPCE from sources

To build IPCE from source, you'll need a few packages:

* nant - common package
* wget - common package
* glitz - common package
* libgail - common package
* svn - common package
* rst2html - in python-docutils, a common package
* mono-devel - common package

In addition, you may need the following for running various parts:

* mono-core - common package
* libgdiplus-devel - needed for Windows.Forms

First, get the sources. You can read about various means here. To get directly from svn:

svn co https://fepy.svn.sourceforge.net/svnroot/fepy/

Next, make the website materials:

cd fepy/website
ln -s ../trunk/doc
make

Next, you need to download all of the pieces. If you want to build IronPython2 Beta2, edit ./download.sh to get it rather than Beta1.

cd ../IPCE
./download.sh
./update.sh

Download mono-1.9 or greater and install.

You may need to set your PATH, LD_LIBRARY_PATH, and PKG_CONFIG_PATH to include the locations of Mono 1.9.

export PATH=/opt/bin/:$PATH
export PKG_CONFIG_PATH=/opt/lib/pkgconfig/:$PKG_CONFIG_PATH
export LD_LIBRARY_PATH=/opt/lib:$LD_LIBRARY_PATH

If you want to build IPCE to use IronPython2 Beta2, change IRON2_VERSION to be 2.0B2 add the following to build.sh:

...
echo 'Unpacking IronPython 2'
unzip -q files/IronPython-$IRON2_VERSION-Src.zip

# Add this:
echo 'Getting IP2 Patches'
rm -rf fepy/patches/2.0b2
(cd fepy/patches; svn export http://svn.cs.brynmawr.edu/Myro/trunk/fepy/IPCE/fepy/patches/2.0b2/)
...

Finally, you are ready to build IPCE IronPython from the fepy/IPCE directory:

./build.sh

This will construct ipy.exe and ipyw.exe in the IPCE/IronPython-1.1/, IPCE/IronPython-2.0A1/, or IPCE/IronPython-2.0B2/ subdirectories. You can run them with:

mono ipy.exe

Press CONTROL+d ENTER to exit IronPython.

To use these DLLs in running or compiling a program, you will have to add it to the environment.

In Visual Studio that will be:

FIXME

In Mono that will be:

FIXME

Using C# inside IronPython

Mono, compile with generics:

gmcs HelloWorld.cs

Use inside IronPython:

 >>> import clr
 >>> clr.AddReference("HelloWorld.exe")
 >>> import HelloWorld
 >>> HelloWorld.Main()

Building a DLL (without a Main):

 gmcs /nologo /t:library /out:echo.dll echo.cs
 gmcs /out:hello.exe /r:echo.dll hello.cs

Hosting

I got this to work with:

$ mono --version
Mono JIT compiler version 1.9.1 (tarball)
Copyright (C) 2002-2007 Novell, Inc and Contributors. www.mono-project.com
        TLS:           __thread
        GC:            Included Boehm (with typed GC)
        SIGSEGV:       normal
        Notifications: epoll
        Architecture:  x86
        Disabled:      none

$ mono ipyw.exe 
Traceback (most recent call last):
  File Snippets, line unknown, in Initialize
ImportError: No module named fepy
IronPython 2.0 Beta (0.0.0.0) on .NET 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
>>> 
using System;
using IronPython.Hosting;
using IronPython.Runtime;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;

public class Engine

{
    public Engine()
    {
	ScriptEngine engine = ScriptRuntime.Create().GetEngine("py");
	ScriptScope scope = engine.CreateScope();
	ScriptSource source = engine.CreateScriptSourceFromString("1 + 1", 
					     SourceCodeKind.Expression);
	int result = source.Execute<int>(scope);
    }

    public static void Main(){
	Engine engine = new Engine();
    }

}

See also examples at http://www.ironpython.info/index.php/Hosting_IronPython_2

using System;
using IronPython.Hosting;
using IronPython.Runtime;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;


public class Engine
{
    private ScriptEngine engine;
    private ScriptScope scope;
    
    public Engine()
    {
	engine = ScriptRuntime.Create().GetEngine("py");
	scope = engine.CreateScope();
	Console.WriteLine(evaluate("23", "def add(y): return x + y"));
    }
    
    public string evaluate(string x, string code) 
    {
	scope.SetVariable("x", x);
	
	try {
	    ScriptSource source = engine.CreateScriptSourceFromString(code, SourceCodeKind.Statements);
	    source.Execute(scope);
	} catch {
	    return "Error executing code";
	}
	
	if (!scope.VariableExists("x"))
            {
                return "x was deleted";
            }
	string result = scope.GetVariable<object>("x").ToString();
	return result;
    }
    
    public static void Main(){
	Engine engine = new Engine();
    }

}

Languages in Linux

To use other human languages in Linux:

yum install iiimf-gtk iiimf-x iiimf-gnome-im-switcher iiimf-libs iiimf-le-canna iiimf-server iiimf-csconv 

To prevent mono from attempting to use XAP for English:

XMODIFIERS=  mono