[Python.NET] Making progress on embedding.

Sam Iredale gyrepin at techie.com
Fri Aug 6 07:56:04 CEST 2004


Still working on the problem of embedding Python for application
scripting in a C# project. I'm having problems getting the Python script
to call back into the C# code, though the application successfully loads
the Python script module. My C# code has evolved to this:

using System;
using Python.Runtime;

namespace PyTest
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	public class PyTestClass
	{
		public PyObject module = null;

		public PyTestClass()
		{
			PythonEngine.Initialize();
		}
		~PyTestClass()
		{
			PythonEngine.Finalize();
		}

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			try
			{
				PyTestClass pyTest = new PyTestClass();
				if(!PythonEngine.IsInitialized)
					throw new
Exception("PythonEngine not initialized.");

				pyTest.module =
PythonEngine.ImportModule("PyTest");
			}
			catch(Exception e)
			{
				Console.WriteLine(e.Message);
				Console.Write(e.StackTrace);
			}
		}
	}

	public class PyTestInterface
	{
		public PyTestInterface()
		{
			// do nothing, just making a public constructor
		}
		public void PyTestMsg()
		{
			Console.WriteLine("Test Pass!");
		}
	}
}

And my Python script has evolved to this:

import CLR
from CLR.PyTest import PyTestInterface

def test():
   print "testing."
   obj=PyTestInterface()
   obj.PyMessage()

test()


Any help would be ... well ... very ... helpful.

Thanks,
SI



More information about the PythonDotNet mailing list