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

Chokobar
  • 29
  • 5
  • 1
    that looks like a placeholder name for an example tutorial... – Alan Kavanagh Jul 05 '16 at 11:14
  • when you type scope. - does it not auto-fill potential methods to be called? surely SomeFunction() is not an actual method – Alan Kavanagh Jul 05 '16 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. – Chokobar Jul 06 '16 at 02:25
  • 1
    oh how could i miss this , both 2 comments are you tehjoker. ^^ – Chokobar Jul 06 '16 at 02:37
  • if you remove scope.SomeFuntion() it should run – Alan Kavanagh Jul 06 '16 at 02:39
  • when i removed nothing happend. Now, I can run python from c# (I mean in normal case) but my python gives the csv file. From my guess , this is the problem because python needs to know where to keep the file, isn't it? – Chokobar Jul 06 '16 at 02:56
  • The (@"C:/PathToPython/script.py") needs to be updated to whichever script you want to run.. But why are you learning to code Python this way? Why not just run the python script in Python interpreter normally? – Alan Kavanagh Jul 06 '16 at 02:59
  • http://stackoverflow.com/q/1601364 – Alan Kavanagh Jul 06 '16 at 03:02
  • My own path is correct sorry for that. The reason I have to use this because my project is webapp and my advisor told me that python is hard to do UI and he told me to do this way instead. – Chokobar Jul 06 '16 at 03:07
  • your mentioned post doesn't work for me :( – Chokobar Jul 06 '16 at 04:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116549/discussion-between-tehjoker-and-chokobar). – Alan Kavanagh Jul 06 '16 at 09:17

1 Answers1

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.

Chokobar
  • 29
  • 5
  • 1
    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 – Alan Kavanagh Jul 06 '16 at 08:27
  • 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. – Chokobar Jul 06 '16 at 09:16