public int ExecuteScript(string sourcePath) { try { var engine = CreateEngine(); var scope = SetupEnvironment(engine); scope.SetVariable("__file__", sourcePath); //var script = engine.CreateScriptSourceFromString(source, SourceCodeKind.Statements); var script = engine.CreateScriptSourceFromFile(sourcePath, Encoding.UTF8, SourceCodeKind.Statements); // setting module to be the main module so __name__ == __main__ is True var compiler_options = (PythonCompilerOptions)engine.GetCompilerOptions(scope); compiler_options.ModuleName = "__main__"; compiler_options.Module |= IronPython.Runtime.ModuleOptions.Initialize; // Setting up error reporter and compile the script var errors = new ErrorReporter(); var command = script.Compile(compiler_options, errors); if (command == null) { return((int)Result.Cancelled); } try { script.Execute(scope); return((int)Result.Succeeded); } catch (SystemExitException) { // ok, so the system exited. That was bound to happen... return((int)Result.Succeeded); } catch (Exception exception) { string _dotnet_err_message = exception.ToString(); string _ipy_err_messages = engine.GetService <ExceptionOperations>().FormatException(exception); // Print all errors to stdout and return cancelled to Revit. // This is to avoid getting window prompts from Revit. // Those pop ups are small and errors are hard to read. _ipy_err_messages = _ipy_err_messages.Replace("\r\n", "\n"); _dotnet_err_message = _dotnet_err_message.Replace("\r\n", "\n"); _message = _ipy_err_messages + "\n\n" + _dotnet_err_message; return((int)Result.Failed); } finally { engine.Runtime.Shutdown(); engine = null; } } catch (Exception ex) { _message = ex.ToString(); return((int)Result.Failed); } }