Hi,

I want to set the __name__ variable to "__main__" in the script I execute. 
However, it seems that the __name__ variable always is overridden with the 
filename of the executed file.

The attached example program gives the output "name: test" instead of "name: 
__main__".

Googling brought up the following issues:

http://ironpython.codeplex.com/workitem/2537 - Here, they use the 
PythonEngine.DefaultModule API which I cannot find.


Best regards

Markus Schaber
-- 
___________________________
We software Automation.

3S-Smart Software Solutions GmbH
Markus Schaber | Developer
Memminger Str. 151 | 87439 Kempten | Germany | Tel. +49-831-54031-0 | Fax 
+49-831-54031-50

Email: m.scha...@3s-software.com | Web: http://www.3s-software.com 
CoDeSys internet forum: http://forum.3s-software.com
Download CoDeSys sample projects: 
http://www.3s-software.com/index.shtml?sample_projects

Managing Directors: Dipl.Inf. Dieter Hess, Dipl.Inf. Manfred Werner | Trade 
register: Kempten HRB 6186 | Tax ID No.: DE 167014915 



using System;
using System.IO;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;

namespace IronPythonNameTest
{
        public static class ScriptExecutor
        {

                private static void Main()
                {
                        try
                        {
                                using (StreamWriter writer = new 
StreamWriter("test.py"))
                                {
                                        writer.WriteLine("print 'name:', 
__name__");
                                }

                                ScriptEngine engine = Python.CreateEngine();

                                ScriptScope mainScope = engine.CreateScope();
                                mainScope.SetVariable("__name__", "__main__");

                                ScriptSource scriptSource = 
engine.CreateScriptSourceFromFile("test.py");

                                scriptSource.Compile();
                                ScriptSource source = scriptSource;

                                source.Execute(mainScope);
                        }
                        finally
                        {
                                Console.WriteLine("Script Finished");
                                Console.ReadLine();
                        }
                }
        }
}


_______________________________________________
Ironpython-users mailing list
Ironpython-users@python.org
http://mail.python.org/mailman/listinfo/ironpython-users

Reply via email to