[Python.NET] Embedded scripting

Sam Iredale gyrepin at techie.com
Fri Aug 6 04:22:49 CEST 2004


Good idea; but it didn't work. :P

I also tried changing the import line AND changing the name of my
namespace to CLR.PyTest. But to no avail. Would the import line even
work with a class that's part of the exe that initialized the Python
interpreter; or am I barking up the wrong tree?

Thanks,
SI

-----Original Message-----
From: Brian Lloyd [mailto:brian at zope.com] 
Sent: Thursday, August 05, 2004 12:49 PM
To: Sam Iredale; pythondotnet at python.org
Subject: RE: [Python.NET] Embedded scripting


Hi Sam - I think the problem is the import line in your 
Python script:

from PyTest.PyTest import PyTestClass

should be:

import CLR # for safety due to a deep import issue
from CLR.PyTest.PyTest import PyTestClass


IOW, all imports from managed code come from the magic 
top-level 'CLR' module.

Brian Lloyd        brian at zope.com
V.P. Engineering   540.361.1716              
Zope Corporation   http://www.zope.com 


> -----Original Message-----
> From: pythondotnet-bounces at python.org 
> [mailto:pythondotnet-bounces at python.org]On Behalf Of Sam Iredale
> Sent: Thursday, August 05, 2004 1:31 PM
> To: pythondotnet at python.org
> Subject: [Python.NET] Embedded scripting
> 
> 
> I'm trying to embed Python for application scripting in my C# 
> solution. I've managed to load the Python module form C#; but how do I

> get the Python script to call back into the application? The "from 
> MODULE import CLASS" semantics throw an exception here; and I think 
> it's because the class being called is in the same process (not in a 
> dll).
> 
> Here's the test C# code:
> 
> using System;
> using Python.Runtime;
> 
> namespace PyTest
> {
> 	/// <summary>
> 	/// Summary description for Class1.
> 	/// </summary>
> 	public class PyTestClass
> 	{
> 		PyObject module = null;
> 
> 		public PyTestClass()
> 		{
> 			PythonEngine.Initialize();
> 		}
> 		~PyTestClass()
> 		{
> 			PythonEngine.Finalize();
> 		}
> 
> 		public void PyMessage()
> 		{
> 			Console.WriteLine("PyMessage() has been
> called.");
> 		}
> 
> 		/// <summary>
> 		/// The main entry point for the application.
> 		/// </summary>
> 		[STAThread]
> 		static void Main(string[] args)
> 		{
> 			PyTestClass pyTest = new PyTestClass();
> 
> 			try
> 			{
> 				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);
> 			}
> 		}
> 	}
> }
> 
> And the Python script:
> 
> from PyTest.PyTest import PyTestClass
> def test():
>    print "testing."
>    PyTestClass.PyMessage()
> test();
> 
> 
> I know the embedding is working; because when the module is loaded, if

> I comment out the import line and the call to the PyTestClass in the 
> script, the string "testing." is output to the console. But once I put

> in the stuff that calls back into the C# code, it throws a 
> PythonException. Any ideas?
> 
> Thanks,
> SI
> 
> _________________________________________________
> Python.NET mailing list - PythonDotNet at python.org 
> http://mail.python.org/mailman/listinfo/pythondotnet
> 



More information about the PythonDotNet mailing list