[Ironpython-users] Setting __name__ to __main__

Markus Schaber m.schaber at 3s-software.com
Thu Nov 24 14:01:39 CET 2011


First, Sorry to all users which I mailed personally, this was not intentional. It seems that, even after several years of being forced to use it, Outlook and me are still not compatible. :-(

Von: Markus Schaber 

> 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.

It seems I found a way to do it:

	ScriptEngine engine = Python.CreateEngine();
	ScriptScope mainScope = engine.CreateScope();

	ScriptSource scriptSource = engine.CreateScriptSourceFromFile("test.py", Encoding.Default, SourceCodeKind.File);

	PythonCompilerOptions pco = (PythonCompilerOptions) engine.GetCompilerOptions(mainScope);

	pco.ModuleName = "__main__";
	pco.Module |= ModuleOptions.Initialize;

	CompiledCode compiled = scriptSource.Compile(pco);

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.schaber at 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 

More information about the Ironpython-users mailing list