|
| 1 | +pythonnet - Python for .NET |
| 2 | +=========================== |
| 3 | + |
| 4 | +|Join the chat at https://gitter.im/pythonnet/pythonnet| |
| 5 | + |
| 6 | +|appveyor shield| |travis shield| |codecov shield| |
| 7 | + |
| 8 | +|license shield| |pypi package version| |python supported shield| |
| 9 | +|stackexchange shield| |
| 10 | + |
| 11 | +Python for .NET is a package that gives Python programmers nearly |
| 12 | +seamless integration with the .NET Common Language Runtime (CLR) and |
| 13 | +provides a powerful application scripting tool for .NET developers. It |
| 14 | +allows Python code to interact with the CLR, and may also be used to |
| 15 | +embed Python into a .NET application. |
| 16 | + |
| 17 | +Calling .NET code from Python |
| 18 | +----------------------------- |
| 19 | + |
| 20 | +Python for .NET allows CLR namespaces to be treated essentially as |
| 21 | +Python packages. |
| 22 | + |
| 23 | +.. code-block:: |
| 24 | +
|
| 25 | + import clr |
| 26 | + from System import String |
| 27 | + from System.Collections import * |
| 28 | +
|
| 29 | +To load an assembly, use the ``AddReference`` function in the ``clr`` |
| 30 | +module: |
| 31 | + |
| 32 | +.. code-block:: |
| 33 | +
|
| 34 | + import clr |
| 35 | + clr.AddReference("System.Windows.Forms") |
| 36 | + from System.Windows.Forms import Form |
| 37 | +
|
| 38 | +Embedding Python in .NET |
| 39 | +------------------------ |
| 40 | + |
| 41 | +- All calls to python should be inside a |
| 42 | + ``using (Py.GIL()) {/* Your code here */}`` block. |
| 43 | +- Import python modules using ``dynamic mod = Py.Import("mod")``, then |
| 44 | + you can call functions as normal, eg ``mod.func(args)``. |
| 45 | +- Use ``mod.func(args, Py.kw("keywordargname", keywordargvalue))`` or |
| 46 | + ``mod.func(args, keywordargname: keywordargvalue)`` to apply keyword |
| 47 | + arguments. |
| 48 | +- All python objects should be declared as ``dynamic`` type. |
| 49 | +- Mathematical operations involving python and literal/managed types |
| 50 | + must have the python object first, eg. ``np.pi * 2`` works, |
| 51 | + ``2 * np.pi`` doesn’t. |
| 52 | + |
| 53 | +Example |
| 54 | +~~~~~~~ |
| 55 | + |
| 56 | +.. code-block:: csharp |
| 57 | +
|
| 58 | + static void Main(string[] args) |
| 59 | + { |
| 60 | + using (Py.GIL()) |
| 61 | + { |
| 62 | + dynamic np = Py.Import("numpy"); |
| 63 | + Console.WriteLine(np.cos(np.pi * 2)); |
| 64 | +
|
| 65 | + dynamic sin = np.sin; |
| 66 | + Console.WriteLine(sin(5)); |
| 67 | +
|
| 68 | + double c = np.cos(5) + sin(5); |
| 69 | + Console.WriteLine(c); |
| 70 | +
|
| 71 | + dynamic a = np.array(new List<float> { 1, 2, 3 }); |
| 72 | + Console.WriteLine(a.dtype); |
| 73 | +
|
| 74 | + dynamic b = np.array(new List<float> { 6, 5, 4 }, dtype: np.int32); |
| 75 | + Console.WriteLine(b.dtype); |
| 76 | +
|
| 77 | + Console.WriteLine(a * b); |
| 78 | + Console.ReadKey(); |
| 79 | + } |
| 80 | + } |
| 81 | +
|
| 82 | +Output: |
| 83 | + |
| 84 | +.. code:: |
| 85 | +
|
| 86 | + 1.0 |
| 87 | + -0.958924274663 |
| 88 | + -0.6752620892 |
| 89 | + float64 |
| 90 | + int32 |
| 91 | + [ 6. 10. 12.] |
| 92 | +
|
| 93 | +Information on installation, FAQ, troubleshooting, debugging, and |
| 94 | +projects using pythonnet can be found in the Wiki: |
| 95 | + |
| 96 | +https://github.com/pythonnet/pythonnet/wiki |
| 97 | + |
| 98 | +.. |Join the chat at https://gitter.im/pythonnet/pythonnet| image:: https://badges.gitter.im/pythonnet/pythonnet.svg |
| 99 | + :target: https://gitter.im/pythonnet/pythonnet?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge |
| 100 | +.. |appveyor shield| image:: https://img.shields.io/appveyor/ci/pythonnet/pythonnet/master.svg?label=AppVeyor |
| 101 | + :target: https://ci.appveyor.com/project/pythonnet/pythonnet/branch/master |
| 102 | +.. |travis shield| image:: https://img.shields.io/travis/pythonnet/pythonnet/master.svg?label=Travis |
| 103 | + :target: https://travis-ci.org/pythonnet/pythonnet |
| 104 | +.. |codecov shield| image:: https://img.shields.io/codecov/c/github/pythonnet/pythonnet/master.svg?label=Codecov |
| 105 | + :target: https://codecov.io/github/pythonnet/pythonnet |
| 106 | +.. |license shield| image:: https://img.shields.io/badge/license-MIT-blue.svg?maxAge=3600 |
| 107 | + :target: ./LICENSE |
| 108 | +.. |pypi package version| image:: https://img.shields.io/pypi/v/pythonnet.svg |
| 109 | + :target: https://pypi.python.org/pypi/pythonnet |
| 110 | +.. |python supported shield| image:: https://img.shields.io/pypi/pyversions/pythonnet.svg |
| 111 | + :target: https://pypi.python.org/pypi/pythonnet |
| 112 | +.. |stackexchange shield| image:: https://img.shields.io/badge/StackOverflow-python.net-blue.svg |
| 113 | + :target: http://stackoverflow.com/questions/tagged/python.net |
0 commit comments