Python Env从C#控制台应用程序激活

发布于 2025-02-01 03:15:04 字数 1078 浏览 3 评论 0原文

我正在尝试从C#Windows Console Application/ Windows Service激活Python Project Env。但是系统无法读取激活文件。如何从C#激活我的Python Project Env?

这是我的代码 -

processstartinfo start = new ProcessStartInfo();

        start.FileName = @"\AppData\Local\Programs\Python\Python37\python.exe";
        start.WorkingDirectory = "path to my python project";
        start.Arguments = ".\\env\\Scripts\\activate && python manage.py runserver";// "env/Scripts/activate.bat && python manage.py runserver";// "E:\\Blockchain\\25.05.2022_Service\\backend\\env\\Scripts\\activate";// && E:\\Blockchain\\07.04.2022 bat file\\First Part\\Authentication\backend & python manage.py runserver
        start.UseShellExecute = false;           
        start.RedirectStandardOutput = true;          

        using (Process process = Process.Start(start))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                string result = reader.ReadToEnd();
                Console.Write(result);
            }
        }

I am trying to activate a python project env from C# windows console application/ windows service. But system cannot read activate file. How to activate my python project env from C#?

Here is my code-

ProcessStartInfo start = new ProcessStartInfo();

        start.FileName = @"\AppData\Local\Programs\Python\Python37\python.exe";
        start.WorkingDirectory = "path to my python project";
        start.Arguments = ".\\env\\Scripts\\activate && python manage.py runserver";// "env/Scripts/activate.bat && python manage.py runserver";// "E:\\Blockchain\\25.05.2022_Service\\backend\\env\\Scripts\\activate";// && E:\\Blockchain\\07.04.2022 bat file\\First Part\\Authentication\backend & python manage.py runserver
        start.UseShellExecute = false;           
        start.RedirectStandardOutput = true;          

        using (Process process = Process.Start(start))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                string result = reader.ReadToEnd();
                Console.Write(result);
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

得不到的就毁灭 2025-02-08 03:15:04

终于我得到了一个解决方案。这是我从C#运行Python Django服务器的完整代码

var process = new Process();

process.StartInfo.FileName = "cmd.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
// directory of project where env folder is exist.
process.StartInfo.WorkingDirectory = workingDirectory;
process.Start();
/* runServerCommand is for the script to run. 
In my case it was- "env\\Scripts\\activate && python manage.py runserver" */
process.StandardInput.WriteLine(runServerCommand);
process.StandardInput.Flush();
process.StandardInput.Close();

using (var reader = process.StandardOutput)
{
    var result = reader.ReadToEnd();
    Console.Write(result);
}

Finally I got a solution. Here is my complete code for running python django server from C#

var process = new Process();

process.StartInfo.FileName = "cmd.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
// directory of project where env folder is exist.
process.StartInfo.WorkingDirectory = workingDirectory;
process.Start();
/* runServerCommand is for the script to run. 
In my case it was- "env\\Scripts\\activate && python manage.py runserver" */
process.StandardInput.WriteLine(runServerCommand);
process.StandardInput.Flush();
process.StandardInput.Close();

using (var reader = process.StandardOutput)
{
    var result = reader.ReadToEnd();
    Console.Write(result);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文