Oscar Johansson
Oscar Johansson

Reputation: 145

C# - Using method from a Python file

I have two files, a regular python (.py) file and a visual studio C# file (.cs). I want to use a function in the python file which returns a string and use that string in the C# file.

Basically:

#This is the Python file!
def pythonString():
   return "Some String"

and

//This is the C# file!
namespace VideoLearning
{
    string whatever = pythonString(); // This is from the Python file.
}

How can I sort of link these two files together? Thanks.

Upvotes: 2

Views: 893

Answers (2)

den.run.ai
den.run.ai

Reputation: 5943

You can also try pythonnet:

python4.net

Upvotes: 0

Patrick
Patrick

Reputation: 90

Check out IronPython! :)

Good declaration: https://blogs.msdn.microsoft.com/charlie/2009/10/25/running-ironpython-scripts-from-a-c-4-0-program/

Hope this helps, it's pretty ez ;)

var ipy = Python.CreateRuntime();
dynamic test = ipy.UseFile("Test.py");
test.pythonString(); //python-function to execute

just replace "pythonString()" with your function (already did this).

Upvotes: 3

Related Questions