<< This is my python code >>
<< (Step 1) - I am registering the class of this python file for this PythonUtil class by executing this code in the pythonwin.exe interpreter >>


class PythonUtil:
    _public_methods_ = ['funct’]
    _reg_progid_ = "PythonUtil.Utilities"
    _reg_clsid_ = "{73B6791B-C161-4F26-9402-D4AE4BC602AB}"

    def funct(self, val, item=None):
    print “hi”

if __name__=='__main__':
  print "COM server being registered..."
  import win32com.server.register
  win32com.server.register.UseCommandLine(PythonUtil)

output:

com being registered.



<< (step2) - I am having this code in c# >>

private void button_Click(object sender, EventArgs e)
{
object instance =Activator.CreateInstance(Type.GetTypeFromProgID("PythonUtil.Utilities"));
 if (instance != null)
 {
   Type type = instance.GetType();
   for (int y = 0; y < ((checkedListBox1.CheckedItems.Count)); y++)
   {
   object objRet = type.InvokeMember("SwapCaseString", BindingFlags.Default | BindingFlags.InvokeMethod, null, instance, paramList);                   
   }

}

}


<< (step 3) - executing this c# code , allows me to call the function 'funct' and 
It works fine for me >>


-----------    Upto here everything is fine   --------------------

But my query is,

  1. I dont want to do the execution process of (Step 1) , instead I need that to be registering the classID process through C# itself.
  2. Let me make it little bit clear ,
    I am allowed to write the code in python , but I cannot execute it in pythonwin.
    Instead I have a button click in c#, which will first registers that class or executes that python dynamically and Executes in c#
    so for that I need an alternative.

<hope u understand my requirement><please help>

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Are you saying you want to execute a python app from a C# app (with a button-push)?

yes I want to execute the c# application , and the user is not allowed to know about the background python .

yes I want to execute the c# application , and the user is not allowed to know about the background python .

Is there any problem with the understanding , I looking for the solution .could anyone help me out of this.?

Here is a test I made using a console app that spawns python and runs a script that sends me a text message.

It can easily be modified to work in a WinForms app (or other).
One concern I have is this technique will not suppress anything displayed by the python code.

using System;
using System.Diagnostics;

namespace DW_401505_con
{
   class Program
   {
      private static ProcessStartInfo RunPythonScript(string strArgs)
      {
         return new ProcessStartInfo()
         {
            FileName = @"c:\Program Files\IronPython 2.7.1\ipy.exe",
            Arguments = strArgs,
            UseShellExecute = false
         };
      }

      private static void Display(string strMessage)
      {
#if DEBUG
         Console.WriteLine(strMessage);
#endif
      }

      static void Main(string[] args)
      {
         try
         {
            Display("Starting process");
            Process.Start(RunPythonScript(@"c:\science\python\SendText.py"));
            Display("Success!!!");
         }
         catch (Exception exc)
         {
            Display("Execution failed: " + exc.Message);
         }
      }
   }
}

Edited 4 Years Ago by thines01: Changed method name

I am getting errors in this part:

return new ProcessStartInfo()
         {
            FileName = @"C:\Program Files\IronPython 2.6\ipy.exe",
            Arguments = strArgs,
            UseShellExecute = false
         };

errors:
; expected
invalid expression term ","

Edited 3 Years Ago by Dani: Formatting fixed

Remove the line break between IronPython and 2.6.
But note: Your python path will go there.

That's fine.

But I think this is similar to this,

private void button1_Click(object sender, EventArgs e)
{
            Display("Starting process");
            ProcessStartInfo st = new ProcessStartInfo();
            st.FileName = @"C:\Program Files\IronPython2.6\ipy.exe";
            st.Arguments = @"D:\Rakesh-TBU\Backup\pythonworkspace\SOURCE\TA.py";
            st.UseShellExecute = false;
            Process.Start(st);
            Display("Success!!!");
}

Output:
on clicking button event , Ipy.exe console window is opening and didnt execute the python file, but it is just idle and nothing happened and closed automatically in a minute.

As per your previous suggestion , I think line space was not the problem ,
it is showing like ';' expected in all the 4 lines ..

Edited 3 Years Ago by mike_2000_17: Fixed formatting

yes I am using Iron python,but I am unable to feed any keystroke to that console..
I tried ipy.exe manually open to execute the python code(.py file) , but I feel like I am in the wrong path for the usage of the command line input options in th ipy.exe

i typed,
ipy decimal.py


syntax error: unexpected token

Edited 4 Years Ago by surakesh: minor changes was required

Does it do that with all Python code you try to run?

yes for all the file.
I even tried this link http://blog.benhall.me.uk/2008/05/ironpython-classes-within-separate.html
but I am getting the same error as , unexpected token...


I am having the folder named "iron" in the c:
and two files customerfile.py and main.py in folder named "iron"

customerfile.py
class Customer(object):
def PrintHello(self):
print "Hello World!"

main.py
from customerfile import Customer
def main():
print 'Hello World'
c = Customer()
print 'Created Customer'
c.PrintHello()

if __name__ == "__main__":
print 'Hello'
main()


IronPython 2.6.1 (2.6.10920.0) on .NET 2.0.50727.3603
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print sys.path
['.', 'C:\\Program Files\\IronPython 2.6', 'C:\\Program Files\\IronPython 2.6',
'C:\\Program Files\\IronPython 2.6\\Lib', 'C:\\Program Files\\IronPython 2.6\\DL
Ls', 'C:\\Program Files\\IronPython 2.6\\lib\\site-packages', 'C:\\Program Files
\\IronPython 2.6\\lib\\site-packages\\win32', 'C:\\Program Files\\IronPython 2.6
\\lib\\site-packages\\win32\\lib', 'C:\\Program Files\\IronPython 2.6\\lib\\site
-packages\\Pythonwin', 'C:\\Program Files\\Common Files\\dSPACE\\RealTimeTesting
\\1.8\\Python', 'C:\\Program Files\\IronPython 2.6\\lib\\site-packages\\wx-2.8-m
sw-unicode']
>>> import sys
>>> sys.path.append("C:\Program Files\Common Files\dSPACE\Python25\Lib")
>>> import os
>>> sys.path.append("C:\iron")
>>> sys.path.append(os.path.abspath("Customer"))
>>> ipy.exe main.py
File "<stdin>", line 1
ipy.exe main.py

^
SyntaxError: unexpected token 'main'

>>>

I ran that from the command-line and it executed with no problem.
Maybe you have a bad install of Iron Python.
Can you re-install it?

Also, please remember to use CODE tags when posting code.

class Customer(object):
   def PrintHello(self):
      print "Hello World!"
from CustomerFile import Customer

def main():
   print 'Hello World'
   c = Customer()
   print 'Created Customer'
   c.PrintHello()


if __name__ == "__main__":
   print 'Hello'
   main()
This article has been dead for over six months. Start a new discussion instead.