pythonEngine in C#

Discussion in 'Software' started by amit_soni2000, May 28, 2009.

  1. amit_soni2000

    amit_soni2000

    Joined:
    May 27, 2009
    Messages:
    1
    hello

    I am facing problem to execute python code from C#.

    my python file look like this:

    import sys
    from mypac_utility import *

    def checkSyntax(filename):

    print "check Syntax function is called"

    sys.stderr = open('errorlog.txt', 'w')
    py_compile.compile(filename)
    sys.stderr.close()
    sys.stderr = sys.__stderr__

    f = open(filename, 'r')
    try:

    exec f
    except Exception, e:
    pass
    print "ERROR occured in {0}: \"{1}\"".format(filename, e)


    def runScript(filename):

    f = open(filename, 'r')
    try:
    exec f
    except Exception, e:
    pass
    print "ERROR occured in {0}: \"{1}\"".format(filename, e)



    checkSyntax("C:\python\myscript.py")
    runScript("C:\python\myscript.py")


    when i try to execute this from C# i got an error "unmarshallable object". but when i remove the line "py_compile.compile(filename)" it works fine.

    my C# code look like this:

    MemoryStream memStream = new MemoryStream();
    engine.SetStandardError(memStream);
    engine.SetStandardOutput(memStream);
    engine.AddToPath(@"C:\Python");
    engine.AddToPath(@"C:\Python25\Lib");

    engine.ExecuteFile(@"C:\python\check_syntax.py");
    byte[] bt = new byte[4096];
    bt = memStream.ToArray();
    char[] Carray = new char[4096];
    bt.CopyTo(Carray, 0);
    MessageBox.Show(new string(Carray));
     
    amit_soni2000, May 28, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page