Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] Problem with Calling a Python function in C# Application

Started by narasimhakumar_k Oct 14, 2005 at 12:20 AM 0 replies 1.7k views
Original Post
narasimhakumar_k
narasimhakumar_k
I have problem with calling a python function(in .py file) using C# I used PyPortalFun.py def fun(): print "hai" I included the Python.Runtime dll I have written C# code as using System; using Python.Runtime; namespace MainProgram { class PyTestClass { public PyTestClass() { PythonEngine.Initialize(); } [STAThread] static void Main(string[] args) { PyTestClass pyTest = new PyTestClass(); try { if(!PythonEngine.IsInitialized) throw new Exception("PythonEngine not initialized."); PyObject module= PythonEngine.ImportModule("PyPortalFun"); Console.WriteLine(PythonEngine.RunSimpleString("import PyPortalFun.fun()")); } catch(Exception e) { Console.WriteLine(e.Message); Console.Write(e.StackTrace); } } } } I used IroPython ie. I Installed in my system and I included the Python.Runtime dll as reference It is giving me the error File "", line 1 import PyPortalFun.fun() ^ Syntax Error : invalid syntax -1 In the Above program i am loading only the module name as pyPortalFun But i have a problem with calling fun() function in PyPortalFun.py from C# I need a solution for calling Python file functions in C# Code please send me c# source code for calling Python function Thanks.. K.V.N.Kumar Apollo Health Street Limited narasimhakumar_k@apollolife.com Hyderabad
Washu
Washu
Quote:
Original post by narasimhakumar_k
Console.WriteLine(PythonEngine.RunSimpleString("import PyPortalFun.fun()"));

last i checked, that was trying to import that name into the current module (your syntax is wrong there as the error code notes), not call that function.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.