0

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?

Dan Bil
  • 11
  • Is there anything special you'd need beyond [`Process.Start()`](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.start?view=net-6.0)? – BrokenBenchmark Jan 03 '22 at 03:34
  • If you need your executable to be fully self-contained then you need to [embed the Python interpreter](https://docs.python.org/3/extending/embedding.html) in your program. The script can go into the PE image's resource directory as a binary resource. – IInspectable Jan 04 '22 at 12:36

2 Answers2

0

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);
    }

0

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.