2

I'm working for Ironpython. I want to execute python code which is need an area for storing the csv (from running python) and my code is as follows:

using System;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using Microsoft.Build.BuildEngine;
using System.Collections.Generic;
using System.Text;

public class dynamic_demo
{
    static void Main()
    {
        var engine = Python.CreateEngine();
        dynamic scope = engine.CreateScope();

        engine.ExecuteFile(@"C:\PathToFile\script.py", scope);

        scope.SomeFunction();
    }
}

This is an error for Visual Studio 2013

'Microsoft.Scripting.Hosting.ScriptScope' does not contain a definition for 'SomeFunction'

And my references are (For both 2013 and 2015 version)

My References

I'm actually using both Visual Studio 2013 and Visual Studio 2015 for checking the results (don't really have to do this) Thanks for your help. Is there any details you need please tell me.

This is an error for my Visual Studio 2015

11
  • 1
    that looks like a placeholder name for an example tutorial... Commented Jul 5, 2016 at 11:14
  • when you type scope. - does it not auto-fill potential methods to be called? surely SomeFunction() is not an actual method Commented Jul 5, 2016 at 11:14
  • I'm also new in programming . When I want to code , so I've just picked sometimes I'm not even know the meaning. Thus , it became like you said alike tutorial example :(. But sure that I will try to change after i can run the program. For tehjoker there is no auto-fill thank you. I would be glad if you have any recommend. Commented Jul 6, 2016 at 2:25
  • 1
    oh how could i miss this , both 2 comments are you tehjoker. ^^ Commented Jul 6, 2016 at 2:37
  • if you remove scope.SomeFuntion() it should run Commented Jul 6, 2016 at 2:39

1 Answer 1

1

I've already solved this ,thanks a lot tehjoker. The problem is at my python code so bad. when i take this out if __name__ == '__main__': C# can call Ironpython without any error but when I changed this at my original (delete the if code) python can't run. I would be perfect if anyone can describe this for me. In conclusion ,why if __name__ == '__main__': is not work for Ironpython but work for python.

Sign up to request clarification or add additional context in comments.

2 Comments

If name == 'main': tests to see if the script was executed as the main function. ( meaning the first script executed ). You can test this now that your python works by adding: print name to the very first line of your python to script to see what it's set to when called via the C# class
yeah it works for both with If name == 'main': and without If name == 'main': but the first one program return 'press any key to continue' then shut it down.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.