Home > Mobile >  Is it possible to run python script in a standalone .NET application?
Is it possible to run python script in a standalone .NET application?

Time:01-04

For one of my projects, I need to execute a python script with some file paths parameters and can't exactly find any resources on how to include it into my application.

I need the app itself to be self-contained so I can't just reference a local python.exe file. I've read up about IronPython but it's not compatible with python3 and I need to be able to import external libraries. There's really very little info on these topics online and I'm hoping someone with similar experience can explain how they got it to work?

CodePudding user response:

Yes, it is possible.

using System.Diagnostics;

public void StartProcess()
    {
        var processStartInfo = new ProcessStartInfo();
        processStartInfo.FileName = "your_python_path";
        var script = "your_script_path";

        processStartInfo.Arguments = $"\"{script}\"";
        processStartInfo.UseShellExecute = false;
        processStartInfo.CreateNoWindow = true;
        processStartInfo.RedirectStandardOutput = true;
        processStartInfo.RedirectStandardError = true;

        var process = Process.Start(processStartInfo);
    }

CodePudding user response:

You can check the answers at this question to see different examples, I am not sure if it's possible to run a Python script without the exe. You would still need to use a tool like IronPython to do that.

Technically there is a version of IronPython which is compatible with Python3 but it's an alpha and it works with the version 3.4 of Python. Besides the compatibility with Python, you would also need to use the following .NET versions: .NET Framework 4.6, .NET Core 2.1, .NET Core 3.1 and .NET 5.

Here's the version that I was talking about.

  •  Tags:  
  • Related