3

I am hoping that someone might be able to help me out here. I am relatively new to C# and am trying to execute some Python code I wrote from within a C# winform app.

What I would like to do is input a name from a text box within the winform and have it process through the python scripts and return a result in another textbox on the winform.

I have found several good examples of how to execute a single script within C#, but I am having some trouble understanding how I can reference multiple scripts.

As an example, I have one python script that references two other scripts within code

from FindTelephone import *
from FindAddress import *

def createPerson(name)
  telephone = FindTelephone(name)
  address = FindAddress(name)
....

Is there a way to have C# point to a reference of my other python scripts before running my main script?

Thanks in advance for the help.

Marshall

  • 1
    You may want to search for a tool called `Iron Python` – Ian Mar 07 '16 at 16:06
  • 1
    I haven't personally done this, but this looks pretty legit: http://www.codeproject.com/Articles/657698/Python-Visual-Studio-and-Csharp-So-Sweet – Jedediah Mar 07 '16 at 16:07
  • 3
    Possible duplicate of [run a python script from c#](http://stackoverflow.com/questions/11779143/run-a-python-script-from-c-sharp) – Mark Skelton Mar 07 '16 at 16:21
  • IronPython is for using Python on the CLR, meaning you can use existing C# code/libraries in python, not the other way around. If you want to execute a python script from C# check the question linked by @MarkyPython – Irwene Mar 07 '16 at 16:21
  • @Sidewinder94 IronPython works both ways: I know from experience it will let a C# app run python, having used to allow users to enter Python code instead of having to parse mathematical expressions. – RoadieRich Mar 09 '16 at 16:57
  • @RoadieRich Interesting, would you happen to have an example available ? There was no such functionality described in the documentation when I used it (admittedly it was a few years ago) – Irwene Mar 10 '16 at 08:31
  • @Sidewinder94 http://www.codeproject.com/Articles/53611/Embedding-IronPython-in-a-C-Application was the tutorial I used, iirc - this would have been 2012, I think. It was for a company I've moved on from, so no longer have access to the source. – RoadieRich Mar 10 '16 at 16:33
  • @RoadieRich Thanks. I'll have to check what differs between PythonTools and IronPython the next time i'll have to make the tow languages interact. But it's nice to have alternatives. – Irwene Mar 10 '16 at 16:37
  • @Sidewinder94 if it helps, I found an old backup of the source: https://bitbucket.org/RoadieRich/centipede – RoadieRich Mar 10 '16 at 16:50
  • Thanks, I'll check this one out as soon as i'll have some time :) – Irwene Mar 11 '16 at 07:41

1 Answers1

2

Like in these posts?: Call Python function from c# (.NET), run a python script from c#?

Or

You might want to look into pythonnet.

Or

If you want an easyer way of doing this then I recommend using Iron Python in place of normal Python. IronPython is an open-source implementation of the Python programming language which is tightly integrated with the .NET Framework. Means that it is much easyer to use with C# and Windows Forms and looks almost just like normal Python.

Or

For in case you are using Visual Studio, VS has some Python tools that might help you in your quest. Link. You can find more documentation Here This last link is provided by Jedediah from comments so vote his comment up if you liked this last link.

Other handy link: Integrating Python With Other Languages

Community
  • 1
  • 1
Morganis
  • 652
  • 5
  • 13
  • 29
  • [tag:IronPython] should definitely be higher in preference than calling python via Processes. Especially if you want to return anything more than just a number or string. – RoadieRich Mar 09 '16 at 17:04
  • Thanks all for the wonderful help and discussion! Iron Python will get the job done. – MarshallTrufant Mar 14 '16 at 15:09