Running Python Code in .NET Environment without Installing Python

If you don't want to introduce a new language to your production environment you should keep all of your code C#, instead of introducing python.

With that said, you don't need to 'install' the python runtime, but you would need to have a runtime available. If that involves installing a nuget package, some mono implementation, or whatever, you are going to rely on some dependency to interpret the python commands.

Here's an article, that I believe answers your question. How to use a packed python package without installing it


As I mentioned in the comments, the right and better way to do it is to create Restful services over your Python code and make http-requests from the C# code. I don't know how much you know about web-frameworks in Python but there are tons of them that you can use. For your need, I would suggest Flask which is light-weight micro web-framework to create Restful web services.

This is very simple Flask web-service for the example: (you can check a running version of it here, I hosted it on pythonOnEverywhere)

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello from Flask!'

@app.route('/math/add/<int:num1>/<int:num2>')
def add(num1, num2):
    return '%d' % (num1+num2)

This simple service, adds two number and returns the sum of them.

And C# Code:

using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

public class Program
{
    // Always use HttpClient as a singleton object
    public static HttpClient _httpClient = new HttpClient() { BaseAddress = new Uri("http://al1b.pythonanywhere.com") } ;
    public async static Task Main()
    {

        var num1 = 1;
        var num2 = 4;

        Console.WriteLine("Making http-request wait ...\r\n");      

        var mathAddResult = await _httpClient.GetAsync($"/math/add/{num1}/{num2}");

        // 200 = OK
        if(mathAddResult.StatusCode == HttpStatusCode.OK)
        {   
            Console.WriteLine(await mathAddResult.Content.ReadAsStringAsync());
        }
    }
}

The output:

Making http-request wait ... 

5

The running version of code above is now runnable on .NET Fiddle.

TL;DR:

For understanding and learning Flask, take a look on its documentions. (It's short and well). I'm sure you will have complex web-services, such as accepting complex or pocco objects as your web-service inputs and returning complex objects (as json) as web-serivce results.

In that case you need to know how Flask jsonify works, This link will tell you how.

Ok, on the other hand, in your C# application you will have those complex objects and scenarios as well. You need to know how to serialize, deseriaze and etc.

Microsoft did a great job for its tutorials here:

https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client

and

https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netframework-4.8


IronPython is limited compared to running Python with C based libraries needing the Python Interpreter, not the .NET DLR. I suppose it depends how you are using the Python code, if you want to use a lot of third party python libraries, i doubt that IronPython will fit your needs.

What about building a full Python application but running it all from Docker?

That would require your environments to have Docker installed, but you could then also deploy your .NET applications using Docker too, and they would all be isolated and not dirty your 'environment'.

There are base docker images out there that are specifically for Building Python and .NET Project and also for running.