|
| 1 | +Embedding Python into .NET |
| 2 | +========================== |
| 3 | + |
| 4 | +.. warning:: |
| 5 | + Because Python code running under Python.NET is inherently |
| 6 | + unverifiable, it runs totally under the radar of the security |
| 7 | + infrastructure of the CLR so you should restrict use of the Python |
| 8 | + assembly to trusted code. |
| 9 | + |
| 10 | +The Python runtime assembly defines a number of public classes that |
| 11 | +provide a subset of the functionality provided by the Python C-API. |
| 12 | + |
| 13 | +These classes include PyObject, PyList, PyDict, PyTuple, etc. |
| 14 | + |
| 15 | +At a very high level, to embed Python in your application one will need |
| 16 | +to: |
| 17 | + |
| 18 | +- Reference ``Python.Runtime.dll`` (e.g. via a ``PackageReference``) |
| 19 | +- Call ``PythonEngine.Initialize()`` to initialize Python |
| 20 | +- Call ``PythonEngine.ImportModule(name)`` to import a module |
| 21 | + |
| 22 | +The module you import can either start working with your managed app |
| 23 | +environment at the time its imported, or you can explicitly lookup and |
| 24 | +call objects in a module you import. |
| 25 | + |
| 26 | +For general-purpose information on embedding Python in applications, use |
| 27 | +www.python.org or Google to find (C) examples. Because Python.NET is so |
| 28 | +closely integrated with the managed environment, one will generally be |
| 29 | +better off importing a module and deferring to Python code as early as |
| 30 | +possible rather than writing a lot of managed embedding code. |
| 31 | + |
| 32 | +.. note:: |
| 33 | + Python is not free-threaded and uses a |
| 34 | + global interpreter lock to allow multi-threaded applications to interact |
| 35 | + safely with the Python interpreter. Much more information about this is |
| 36 | + available in the Python C-API documentation on the www.python.org |
| 37 | + Website. |
| 38 | + |
| 39 | +When embedding Python in a managed application, one has to manage the |
| 40 | +GIL in just the same way you would when embedding Python in a C or C++ |
| 41 | +application. |
| 42 | + |
| 43 | +Before interacting with any of the objects or APIs provided by the |
| 44 | +``Python.Runtime`` namespace, calling code must have acquired the Python |
| 45 | +global interpreter lock by calling the ``PythonEngine.AcquireLock`` |
| 46 | +method. The only exception to this rule is the |
| 47 | +``PythonEngine.Initialize`` method, which may be called at startup |
| 48 | +without having acquired the GIL. |
| 49 | + |
| 50 | +When finished using Python APIs, managed code must call a corresponding |
| 51 | +``PythonEngine.ReleaseLock`` to release the GIL and allow other threads |
| 52 | +to use Python. |
| 53 | + |
| 54 | +A ``using`` statement may be used to acquire and release the GIL: |
| 55 | + |
| 56 | +.. code:: csharp |
| 57 | +
|
| 58 | + using (Py.GIL()) |
| 59 | + { |
| 60 | + PythonEngine.Exec("doStuff()"); |
| 61 | + } |
| 62 | +
|
| 63 | +The AcquireLock and ReleaseLock methods are thin wrappers over the |
| 64 | +unmanaged ``PyGILState_Ensure`` and ``PyGILState_Release`` functions |
| 65 | +from the Python API, and the documentation for those APIs applies to the |
| 66 | +managed versions. |
| 67 | + |
| 68 | +Passing C# Objects to the Python Engine |
| 69 | +--------------------------------------- |
| 70 | + |
| 71 | +This section demonstrates how to pass a C# object to the Python runtime. |
| 72 | +The example uses the following ``Person`` class: |
| 73 | + |
| 74 | +.. code:: csharp |
| 75 | +
|
| 76 | + public class Person |
| 77 | + { |
| 78 | + public Person(string firstName, string lastName) |
| 79 | + { |
| 80 | + FirstName = firstName; |
| 81 | + LastName = lastName; |
| 82 | + } |
| 83 | +
|
| 84 | + public string FirstName { get; set; } |
| 85 | + public string LastName { get; set; } |
| 86 | + } |
| 87 | +
|
| 88 | +In order to pass a C# object to the Python runtime, it must be converted |
| 89 | +to a ``PyObject``. This is done using the ``ToPython()`` extension |
| 90 | +method. The ``PyObject`` may then be set as a variable in a ``PyScope``. |
| 91 | +Code executed from the scope will have access to the variable: |
| 92 | + |
| 93 | +.. code:: csharp |
| 94 | +
|
| 95 | + // create a person object |
| 96 | + Person person = new Person("John", "Smith"); |
| 97 | +
|
| 98 | + // acquire the GIL before using the Python interpreter |
| 99 | + using (Py.GIL()) |
| 100 | + { |
| 101 | + // create a Python scope |
| 102 | + using (PyScope scope = Py.CreateScope()) |
| 103 | + { |
| 104 | + // convert the Person object to a PyObject |
| 105 | + PyObject pyPerson = person.ToPython(); |
| 106 | +
|
| 107 | + // create a Python variable "person" |
| 108 | + scope.Set("person", pyPerson); |
| 109 | +
|
| 110 | + // the person object may now be used in Python |
| 111 | + string code = "fullName = person.FirstName + ' ' + person.LastName"; |
| 112 | + scope.Exec(code); |
| 113 | + } |
| 114 | + } |
0 commit comments