【问题标题】:How can I Integrate Python version 3 with .Net如何将 Python 版本 3 与 .Net 集成
【发布时间】:2019-04-13 19:11:26
【问题描述】:

我正在将 Python 脚本集成到 .Net,并且我正在使用 Visual Studio 中的 .Net 开发人员可用的 IronPython 包。我的 Python 代码包含 face_recognition、glob、opencv 等包。尽管简单的 Python 在 PyCharm 中运行,但在运行此 python 脚本时出现错误。谁能知道我做错了什么?请给我应该做的答案。

我正在使用的这些包,后来我通过调用它们来使用它:

import face_recognition
import cv2
import glob

video_capture = cv2.VideoCapture(0)

all_images = glob.glob('images/*.jpg')

这是我在 Visual Studio 中的代码:(制作控制台应用程序)

            var py = Python.CreateEngine();

            py.ExecuteFile("C:\\Users\\Hp\\PycharmProjects\\final_face\\example.py");
            Console.ReadLine();

我在 Visual Studio 中得到的错误是:

Exception thrown: 'Microsoft.Scripting.SyntaxErrorException' in Microsoft.Scripting.dll

The program '[23408] PythonDotNet.exe' has exited with code 0 (0x0).

【问题讨论】:

  • 您是否对本机 (CPython) 和 IronPython 解释器使用相同的 Python 版本? v2 和 v3 之间存在许多差异。
  • 不,IronPython 最新版本是 2.7.9,CPython 是 3.6.6。
  • 那你发现问题了,IronPython 3 还没有准备好生产,见github.com/IronLanguages/ironpython3
  • 您可以在命令行中执行 Python 脚本并解析输出,而不是使用 IronPython,看看这是否有帮助:stackoverflow.com/questions/206323/…
  • @AlexanderPope 谢谢你建议的链接有我的答案。我在下面回答我的问题。

标签: c# python .net opencv face-recognition


【解决方案1】:

您收到错误,因为该功能不是您想要的。

创建一个名为 RunPython.csproj 的新控制台应用程序项目。在主子中,写:

static void Main(string[] args)
{
    var py = Python.CreateRuntime();
    py.ExecuteFile();
}

之后,您可以构建您的项目以生成“.exe”文件。在 Console 或 shell 中,编写:

RunPython.exe "C:\Users\Hp\PycharmProjects\final_face\example.py"

您可以访问this site 了解更多示例。

【讨论】:

  • 不,这不是工作人员。它抛出错误并在错误列表中显示构建失败。
【解决方案2】:

IronPython 3 尚未准备好投入生产,因此您可以在命令行中执行 Python 脚本并解析输出,而不是使用 IronPython:

  class Program
      {
          static void Main(string[] args)
          {
              Program p = new Program();
              p.butPython();
          }

      public void butPython()
      {
          var hello = "Calling Python...";

          Tuple<String, String> python = GoPython(@"C:\Users\HP\PycharmProjects\final_face\final.py");
          hello = python.Item1; // Show result.
          Console.WriteLine(hello);
          Console.ReadLine();
      }

      public Tuple<String, String> GoPython(string pythonFile, string moreArgs = "")
      {
          ProcessStartInfo PSI = new ProcessStartInfo();
          PSI.FileName = "py.exe";
          PSI.Arguments = string.Format("\"{0}\" {1}", pythonFile, moreArgs);
          PSI.CreateNoWindow = true;
          PSI.UseShellExecute = false;
          PSI.RedirectStandardError = true;
          PSI.RedirectStandardOutput = true;
          using (Process process = Process.Start(PSI))
          using (StreamReader reader = process.StandardOutput)
          {
              string stderr = process.StandardError.ReadToEnd(); // Error(s)!!
              string result = reader.ReadToEnd(); // What we want.
              return new Tuple<String, String>(result, stderr);
          }
      }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-12
    • 2017-02-15
    • 2019-09-09
    • 2012-04-16
    • 1970-01-01
    • 2011-05-21
    • 2013-01-24
    相关资源
    最近更新 更多