'Iron Python, trouble using SetVariable

For Context: I am working on a project to allow python scripts given by clients to be executed with our driver (c#) so need to see if I can import their file, set variables for it in our driver and then execute their file.

I have an imported python file and I also have it as a string to see if it was the file causing problems.

The problem I am facing is this. .SetVariable will only work if the variable I am assigning it to has been declared but not set. despite the documentation saying "These methods assign a value to a variable in the scope, overwriting any previous value" - https://documentation.help/IronPython/scopes.html

For clarity, I am trying to replace the python variable super_string but it will not do so unless it is set to nothing. example if my python file contains: super_string it will work but super_string = "my super string" will not as it is not empty

    public static void runTime()
        {
            //create Engine
            var engine = Python.CreateEngine();
            //string of script
            string testClass = @"

super_string = 'my super string'

class Test_Class:
    def __init__(self,word):
        self.word = word
    def f(self):
        return 'Hello World in Python'
    def word_function(self):
        return 'Hello World ' + self.word

test_instance = Test_Class({0})";

            //file of script
            string fileName = "../../python_Script.py";
            ScriptSource pythonFile = engine.CreateScriptSourceFromFile(fileName);

            //scopes
            var scope = engine.CreateScope();
            var fileScope = engine.CreateScope();

            //set variable of scopes
            scope.SetVariable("super_string", 42);
            fileScope.SetVariable("super_string", 42);

            //Execute
            engine.Execute(testClass, scope);
            pythonFile.Execute(fileScope);


            Console.WriteLine(fileScope.GetVariable("super_string"));
            Console.WriteLine(scope.GetVariable("super_string"));
        }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source