C # calls the arcpy method

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.