When it comes to Python, many people should like it very much. Python syntax is concise and clear, with a rich and powerful class library, it is easy to put in other languages to make a variety of modules. But because he is too simple, sometimes in the other software when the combination will encounter problems. So we often wonder if there are some simple ways to do this, and then Python's resources can be used directly in other languages. For. NET platform, the answer is yes.
Visual Studio as a Python compiler for Visual Studio should be a very powerful IDE environment, and it itself provides support for Python. https://pytools.codeplex.com/can download the PTVs tool. With this tool from Microsoft, we are able to take full advantage of Visual Studio as a python working environment for easy implementation of Visual Studio support for other languages, such as code writing, debugging, unit testing, and so on. Other examples like Microsoft Cloud Platform Azure also offer Python SDK.
. NET platform code directly calling Python's code with the Python IDE environment in Visual Studio does not mean that we can easily integrate Python and C #. But Microsoft offers another Open-source tool, IronPython. Using this tool is very simple, follow these steps:
1 Install IronPython from the above website. 2 Create a Python file, as shown below. The following references Sys.path because the address of the Python class library is required in the. NET environment, otherwise the call fails.
Import sys
sys.path.append ("C:\Python27")
sys.path.append ("C:\Python27\DLLs")
sys.path.append ("C:\ Python27\lib ")
sys.path.append (" C:\Python27\Lib\site-packages ")
Sys.platform =" Win32 "
class Firstapp :
def do (self): return
["Hello, world!", "Alice"]
3 Create a new project that quotes the fate of Ironpython.hosting and Microsoft.Scripting.Hosting. 4 loading Python classes in C # code
var engine = Python.createengine ();
Dynamic py = engine. Executefile (@ "firstapp.py");
Dynamic Firstapp = py. Firstapp ();
5 Call a class function in Python directly in C # code
IronPython.Runtime.List output = <span style= "FONT-SIZE:12PX; Font-family:arial, Helvetica, Sans-serif; " >firstapp</span><span style= "FONT-SIZE:12PX; Font-family:arial, Helvetica, Sans-serif; " ". Do ();</span>
The entire implementation process is very simple, but one should worry about the performance of the call across languages. In fact, the principle of IronPython is to compile Python into the intermediate language il of the. NET platform, so it's more efficient to keep your code running. Interested can look at Microsoft's official interpretation of the article.
The experiment found that most of the time IronPython can work, but in a few cases Python's class library may rely on some of the older C + + code, when IronPython error occurs. This is the time to consider another open source solution to this problem, namely ironclad. Specific issues need specific analysis, this blog does not elaborate too much.