4

Consider the code below:

ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null);
ScriptRuntime runtime = new ScriptRuntime(setup);
ScriptEngine engine = Python.GetEngine(runtime);
ScriptScope scope = engine.CreateScope();

scope.SetVariable("message", "Hello, world!");
string script = @"print message";
ScriptSource source = scope.Engine.CreateScriptSourceFromString(script, SourceCodeKind.Statements);
source.Execute();

This code yields the following exception:

Microsoft.Scripting.Runtime.UnboundNameException was unhandled Message="name 'message' is not defined"

What am I missing?

2 Answers 2

7

It should be "source.Execute(scope);" instead of "source.Execute();"

Sign up to request clarification or add additional context in comments.

Comments

3

Found this: A 3 minute guide to embedding IronPython in a C# application.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.