首页 > 解决方案 > 在 .Net Core 上需要在 main.py 脚本之前运行 python 命令

问题描述

这是问题所在。

我需要在 ubuntu WSL 上使用 C# .Net core 运行一些 python 文件。(在 Windows 机器上安装 Ubuntu 并在 Ubuntu 中运行 C# dll,使用里面DotNet file.dll运行 python 文件)

在实际 main.py 文件运行之前,我需要运行以下命令。

python3 -m pip install --user virtualenv 
python3 -m venv env 
pip install -r requirements.txt
source env/bin/activate

我在 C# 中使用下面的代码来运行 python

ProcessStartInfo start = new ProcessStartInfo
            {
                FileName = "main.py",
                UseShellExecute = false,
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
            };

using (Process process = Process.Start(start))
            {
                using (StreamWriter reader = process.StandardInput) {
                    reader.WriteLine("pip install --upgrade pip");
                }
                using (StreamReader reader = process.StandardOutput)
                {
                    //result = reader.ReadToEnd();
                    process.WaitForExit();
                    Task.Delay(TimeSpan.FromSeconds(90)).Wait();
                }
            }

但它总是失败。任何建议高度赞赏。

标签: c#python-3.x.net-coreubuntu-18.04

解决方案


推荐阅读