So I have installed IronPython 3.4 to replace the IronPython 2.7 I was using. The code below worked fine in 2.7 but when I use it in 3.4 I am getting the error: Microsoft.Scripting.SyntaxErrorException: 'invalid syntax' Any ideas? Thanks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
Microsoft.Scripting.Hosting.ScriptEngine pythonEngine = IronPython.Hosting.Python.CreateEngine();
Microsoft.Scripting.Hosting.ScriptSource pythonScript = pythonEngine.CreateScriptSourceFromString("print 'Hello World!'");
pythonScript.Execute();
}
}
}
Tried the code above and it didn't work but worked in IronPython 2.7.
CodePudding user response:
IronPython 3.4
uses python 3.4 syntax where as IronPython 2.7
uses python 2.7.
In python 2.7 print
is a statement, where as in python 3 print
is a function(which means you need to use parentheses to wrap the arguments). So to make your code work you have to change the following section from
pythonEngine.CreateScriptSourceFromString("print 'Hello World!'");
^^^^^^^^^^^^^^^^^^^^
to
pythonEngine.CreateScriptSourceFromString("print('Hello World!')");
^^^^^^^^^^^^^^^^^^^^^