Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IronPython: Unexpected token 'from'

i was running python script from .net using IronPython, below is my python script

import tensorflow as tf    
print('Tensorflow Imported')

below is C# Code

using System;
using System.Text;
using System.IO;
using IronPython.Hosting;
using System.Collections.Generic;
using Microsoft.Scripting.Hosting;

namespace ConsoleApplication1
{
    class Program
    {
        private static void Main()
        {
            var py = Python.CreateEngine();
            List<string> searchPaths = new List<string>();
            searchPaths.Add(@"C:\Users\Admin\AppData\Local\Programs\Python\Python35\Lib)");
            searchPaths.Add(@"C:\Users\Admin\AppData\Local\Programs\Python\Python35\Lib\site-packages)");
            py.SetSearchPaths(searchPaths);
            try
            {
                py.ExecuteFile("script.py");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }           
        }
    }
}

below is my output

Unexpected token 'from'

if i remove import statement then python script executes fine. I tried including os,sys all those were imported without any issue. I have installed TensorFlow via pip, when i run above script through python console(v3.5) it works fine.

Update: in TF doc its written "TensorFlow only supports version 3.5.x of Python on Windows". but official release of IronPython is version 2.7 I was happy to find IronPython on GitHub, tried building it (i just typed build in console and got freaked out with the long list of error messages it showed! :D couldn't find pre-compiled binaries

is there any alternative way to import tensorflow in IronPython 2.7 or run Python in .net?

like image 254
Prakash M Avatar asked Oct 17 '22 15:10

Prakash M


1 Answers

Prakash - as you found in the documentation, TensorFlow requires Python 3.5 or 3.6 when running on Windows. It won't run in IronPython 2.7.

One user on GitHub successfully (with a lot of work and in a not-easy-to-do) way got TF running on Windows under Python2.7, and you might be able to build on their work, but it's not exactly the solution you were looking for for IronPython. My best suggestion is to go with 3.5 or 3.6.

like image 53
dga Avatar answered Oct 20 '22 11:10

dga