从C#执行python脚本时无法加载python包

时间:2019-07-01 11:01:49

标签: c# python ironpython

嗨,我是python和ironPython的新手,我正在尝试从c#执行python脚本,我可以成功地从python交互式窗口执行脚本,但是我无法从c#执行脚本。我可以从C#执行基本功能的python脚本,但是我无法执行我的脚本,因为我使用了一些程序包,例如numpy,keras,tensorflow,sk-learn,pandas等,我遇到了这样的错误

Traceback (most recent call last):
File "C:\Users\amarn\Downloads\Projects\AI_Score\Ai_Score\AI_Score.py", line 1, in <module>
import keras
File "C:\Users\amarn\Anaconda3\lib\site-packages\keras\__init__.py", line 3, in <module>
from . import utils
File "C:\Users\amarn\Anaconda3\lib\site-packages\keras\utils\__init__.py", line 2, in <module>
from . import np_utils
File "C:\Users\amarn\Anaconda3\lib\site-packages\keras\utils\np_utils.py", line 6, in <module>
import numpy as np
File "C:\Users\amarn\Anaconda3\lib\site-packages\numpy\__init__.py", line 140, in <module>
from . import _distributor_init
File "C:\Users\amarn\Anaconda3\lib\site-packages\numpy\_distributor_init.py", line 34, in <module>
from . import _mklinit
ImportError: DLL load failed: The specified module could not be found.

所以任何人都可以帮助我做什么,我也想知道为什么这些程序包没有加载从c#代码执行的python.exe。 这是我的C#代码:

public static void RunPythonScript()
    {
        try
        {
            ProcessStartInfo pythonInfo = new ProcessStartInfo();
            pythonInfo.FileName = @"C:\Users\amarn\Anaconda3\python.exe";
            var file1 = "C:/Users/amarn/source/repos/IBBI Crawling And Ai_Score/IBBI Crawling/bin/Debug/InsolvencyCompany_20190627T181821.xlsx".Replace(" ","%20");
            string PredictFile_file2 = "C:/Users/amarn/source/repos/IBBI Crawling And Ai_Score/IBBI Crawling/bin/Debug/InsolvencyCompany_Predict_20190627T181821.xlsx".Replace(" ", "%20");
            var script = string.Format(@"C:\Users\amarn\Downloads\Projects\AI_Score\AI_Score\AI_Score.py {0} {1}", file1, PredictFile_file2);
            pythonInfo.Arguments = script;

            pythonInfo.UseShellExecute = false;
            pythonInfo.CreateNoWindow = true;
            pythonInfo.RedirectStandardOutput = true;
            pythonInfo.RedirectStandardInput = false;
            pythonInfo.RedirectStandardError = true;

            using (Process process = new Process())
            {
                process.StartInfo = pythonInfo;
                process.Start();
                process.WaitForExit();

                var errors = process.StandardError.ReadToEnd();
                var results = process.StandardOutput.ReadToEnd();
                if (!string.IsNullOrEmpty(errors))
                {
                    Console.WriteLine("Erros: " + errors);
                }
                Console.WriteLine("Result: \n" + results);

            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

0 个答案:

没有答案