private void InitPyEngine() { mEngine = Python.CreateEngine(); ScriptScope builtin = Python.GetBuiltinModule(mEngine); builtin.SetVariable("__import__", new ImportDelegate(SharedScopeImport)); mScope = mEngine.CreateScope(); mScope.SetVariable("eve", this); }
static void Main(string[] args) { var filePath = @"D:\ScriptTest\TempDebugScript.py"; // save python script in file using (var sw = File.CreateText(filePath)) { sw.Write(@"User.Age = 15"); } // create object that will be passed to script scope User user = new User() { Age = 5, FirstName = "Radovan" }; // trace Console.WriteLine(user.FirstName); Console.WriteLine(user.Age); // initiating engine and scope ScriptEngine scriptEngine; var options = new Dictionary <string, object>(); options["LightWeightScopes"] = true; scriptEngine = Python.CreateEngine(options); ScriptScope scope = Python.GetBuiltinModule(scriptEngine); scope = scriptEngine.CreateScope(); // adding variable to scope scope.SetVariable("User", user); //executing Python script scriptEngine.ExecuteFile(filePath, scope); // trace Console.WriteLine(user.FirstName); Console.WriteLine(user.Age); Console.ReadLine(); }
public void SetGlobals(object Globals) { Python.GetBuiltinModule(_engine).SetVariable("globals", Globals); }