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