[Python.NET] Problem passing parameters to a python function from C#

Brian Lloyd brian.d.lloyd at gmail.com
Fri May 12 15:08:58 CEST 2006

HI Tiago -

If I'm reading this right, I think the issue is that your method is
a (Python) Template instance.

The PythonNet runtime can't convert arbitrary Python types to anything
that .NET can make sense of, so your method need to either return a
"primitive" Python type that maps to a primitive .NET type (string, int,
etc.), or a wrapped .NET object.

Try returning str(template), and I bet you'll get the string result on the
other side.

Hope this helps,


On 5/10/06, Tiago Matias <tiago.matias at gmail.com> wrote:
> Hi all!
> I'm trying to invoke a python function from a .NET application. The C#
> code is this:
> string templateFile = "Simple.tpl";
>  PythonEngine.Initialize();
> PyObject module = PythonEngine.ImportModule("Engine");
> PyObject method = module.GetAttr("FillTemplate");
> PyObject result = method.Invoke(PyObject.FromManagedObject(templateFile));
> Console.WriteLine(result.ToString());
> and the python module is simply:
> from Cheetah.Template import Template
> def FillTemplate(templateName):
>         template = Template(file=templateName)
>         return template
> Now, the problem is the parameter. If I hardcode "Simple.tpl" in the
> constructor everything works as expected. Also, if I change the python code
> to "return templateName" I get "Simple.tpl" back in the C# code.
> But, if I run the code above a PythonException is thrown....I've also
> tried to place a try...except block around the code but it didn't caught any
> exception... I suspect that I must be calling the PyObject.Invoke()
> function with the wrong arguments...
> Essentially the purpose of this is to generate C# code with the help of
> cheetah templates. The ideia is to construct an object containing the
> metadata and then pass it to cheetah to generate code.
> But I can't seem to be able to pass a simple string!
> Any help would be grealty appreciated! Btw, bear in mind that this is my
> first attempt with python... so the question maybe stupid ;)
> Thanks in advance!
> Tiago
> _________________________________________________
> Python.NET mailing list - PythonDotNet at python.org
> http://mail.python.org/mailman/listinfo/pythondotnet
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/pythondotnet/attachments/20060512/960146e8/attachment.htm 

More information about the PythonDotNet mailing list