c# - IronPython:意外 token 'from'

标签 c# python .net tensorflow ironpython

我使用 IronPython 从 .net 运行 python 脚本,下面是我的 python 脚本

import tensorflow as tf    
print('Tensorflow Imported')

下面是C#代码

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);
            }           
        }
    }
}

下面是我的输出

Unexpected token 'from'

如果我删除 import 语句,则 python 脚本可以正常执行。我尝试包括 os,sys 所有这些都没有任何问题地导入。我已经通过 pip 安装了 TensorFlow,当我通过 python 控制台 (v3.5) 运行上面的脚本时它工作正常。

更新:在TF doc它写着“TensorFlow 仅支持 Windows 上的 Python 3.5.x 版本”。但 IronPython 的官方版本是 2.7 版 我很高兴在 GitHub 上找到 IronPython , 尝试构建它(我只是在控制台中输入构建并被它显示的 long list 的错误消息吓坏了!:D 找不到预编译的二进制文件

是否有其他方法可以在 IronPython 2.7 中导入 tensorflow 或在 .net 中运行 Python?

最佳答案

Prakash - 正如您在文档中所见,TensorFlow 在 Windows 上运行时需要 Python 3.5 或 3.6。它不会在 IronPython 2.7 中运行。

一位用户在 GitHub 上成功地(通过大量工作和不容易做到的方式)got TF running on Windows under Python2.7 ,并且您可能能够在他们的工作的基础上进行构建,但这并不是您正在寻找的 IronPython 解决方案。我最好的建议是使用 3.5 或 3.6。

关于c# - IronPython:意外 token 'from',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43068712/

相关文章:

c# - 奇怪的 wpf 绑定(bind)

python - 将函数传递给类

c# - 监控 .NET ASP.NET 应用程序

c# - 如何访问动态对象上名为 "base"的属性

c# - 如何将配置输出设置到特殊文件夹(LocalApplicationData)?

c# - DataGridViewComboBoxCell : How to set selected value when adding a row?

python - 将 lambda 函数应用于列在 pandas 中失败

python - 计算 .txt 中单词的长度

c# - 如何将下面的 javascript 翻译为 C#?

python - 如何选择合适的 Python 解析器生成器来解析 C 结构体定义?