1, installation Inronpython
To be in. NET environment using Python, first install Inronpython (also install Python, of course): http://ironpython.net/or https://ironpython.codeplex.com/
2. Add Reference Library
After you create a new project in Visual Studio, add references IronPython.dll and Microsoft.Scripting.dll (located in the installation directory of Inronpython).
3. Loading Python code from the file
Create a new Python file in the project, such as hello.py, and set its properties copy to Output directory with the value copy if newer. Write the following code under hello.py:
Def say_hello (): print "hello!" Def get_text (): Return "text from hello.py" def add (Arg1, arg2): return arg1 + arg2
C # code:
var engine = IronPython.Hosting.Python.CreateEngine (); var scope = engine. Createscope (); var source = engine. Createscriptsourcefromfile ("hello.py"); source. Execute (scope); var Say_hello = scope. Getvariable<func<object>> ("Say_hello"); Say_hello (); var get_text = scope. Getvariable<func<object>> ("Get_text"); var text = Get_text (). ToString (); Console.WriteLine (text); var add = scope. Getvariable<func<object, Object, object>> ("Add"), var result1 = Add (1, 2); Console.WriteLine (RESULT1); var result2 = Add ("Hello", "World"); Console.WriteLine (RESULT2);
4. Third-party module issues with Python installation
The Python code file adds the following lines:
Import syssys.path.append (' C:\\python27\\lib ') sys.path.append (' C:\\python27\\lib\\site-packages\\ Setuptools-12.0.3-py2.7.egg ') sys.path.append (' C:\\python27\\lib\\site-packages\\rsa-3.1.1-py2.7.egg ') import RSA
Http://www.cnblogs.com/martianzone/p/3569544.html
http://blog.csdn.net/taonull/article/details/42923015
This article from "It Technology Learning and communication" blog, declined reprint!
C # calls the arcpy method