|
1 |
| -pythonnet |
2 |
| -========= |
| 1 | +# pythonnet - Python for .NET |
3 | 2 |
|
4 |
| -Python for .NET is a package that gives Python programmers nearly seamless integration with the .NET Common Language Runtime (CLR) and provides a powerful application scripting tool for .NET developers. It allows Python code to interact with the CLR, and may also be used to embed Python into a .NET application. |
| 3 | +Python for .NET is a package that gives Python programmers nearly |
| 4 | +seamless integration with the .NET Common Language Runtime (CLR) and |
| 5 | +provides a powerful application scripting tool for .NET developers. |
| 6 | +It allows Python code to interact with the CLR, and may also be used to |
| 7 | +embed Python into a .NET application. |
5 | 8 |
|
6 |
| -[](https://travis-ci.org/pythonnet/pythonnet) |
| 9 | +[![travis shield][]](https://travis-ci.org/pythonnet/pythonnet) |
| 10 | +[![appveyor shield][]](https://ci.appveyor.com/project/pythonnet/pythonnet-0kq5d/branch/master) |
| 11 | +[![license shield][]](./LICENSE) |
7 | 12 |
|
8 |
| -[](https://ci.appveyor.com/project/pythonnet/pythonnet-0kq5d/branch/master) |
| 13 | +## Calling .NET code from Python |
9 | 14 |
|
10 |
| -**Calling .NET code from Python** |
11 |
| - |
12 |
| -Python for .NET allows CLR namespaces to be treated essentially as Python packages. |
| 15 | +Python for .NET allows CLR namespaces to be treated essentially |
| 16 | +as Python packages. |
13 | 17 |
|
14 | 18 | ```python
|
15 |
| - import clr |
16 |
| - from System import String |
17 |
| - from System.Collections import * |
| 19 | +import clr |
| 20 | +from System import String |
| 21 | +from System.Collections import * |
18 | 22 | ```
|
19 |
| -To load an assembly, use the "AddReference" function in the "clr" module: |
| 23 | + |
| 24 | +To load an assembly, use the `AddReference` function in the `clr` module: |
20 | 25 |
|
21 | 26 | ```python
|
22 |
| - import clr |
23 |
| - clr.AddReference("System.Windows.Forms") |
24 |
| - from System.Windows.Forms import Form |
| 27 | +import clr |
| 28 | +clr.AddReference("System.Windows.Forms") |
| 29 | +from System.Windows.Forms import Form |
25 | 30 | ```
|
26 | 31 |
|
27 |
| -**Embedding Python in .NET** |
| 32 | +## Embedding Python in .NET |
| 33 | + |
| 34 | +- All calls to python should be inside |
| 35 | + a `using (Py.GIL()) {/_ Your code here _/}` block. |
| 36 | +- Import python modules using `dynamic mod = Py.Import("mod")`, |
| 37 | + then you can call functions as normal, eg `mod.func(args)`. |
| 38 | +- Use `mod.func(args, Py.kw("keywordargname", keywordargvalue))` |
| 39 | + to apply keyword arguments. |
| 40 | +- All python objects should be declared as `dynamic` type. |
| 41 | +- Mathematical operations involving python and literal/managed types must |
| 42 | + have the python object first, eg `np.pi_2` works, `2_np.pi` doesn't. |
28 | 43 |
|
29 |
| -+ All calls to python should be inside a "using (Py.GIL()) {/* Your code here */}" block. |
30 |
| -+ Import python modules using dynamic mod = Py.Import("mod"), then you can call functions as normal, eg mod.func(args). |
31 |
| -+ Use mod.func(args, Py.kw("keywordargname", keywordargvalue)) to apply keyword arguments. |
32 |
| -+ All python objects should be declared as 'dynamic' type. |
33 |
| -+ Mathematical operations involving python and literal/managed types must have the python object first, eg np.pi*2 works, 2*np.pi doesn't |
| 44 | +### Example |
34 | 45 |
|
35 |
| -EG: |
36 | 46 | ```csharp
|
37 | 47 | static void Main(string[] args)
|
38 | 48 | {
|
39 |
| - using (Py.GIL()) { |
40 |
| - dynamic np = Py.Import("numpy"); |
41 |
| - dynamic sin = np.sin; |
42 |
| - Console.WriteLine(np.cos(np.pi*2)); |
43 |
| - Console.WriteLine(sin(5)); |
44 |
| - double c = np.cos(5) + sin(5); |
45 |
| - Console.WriteLine(c); |
46 |
| - /* this block is temporarily disabled due to regression |
47 |
| - dynamic a = np.array(new List<float> { 1, 2, 3 }); |
48 |
| - dynamic b = np.array(new List<float> { 6, 5, 4 }, Py.kw("dtype", np.int32)); |
49 |
| - Console.WriteLine(a.dtype); |
50 |
| - Console.WriteLine(b.dtype); |
51 |
| - Console.WriteLine(a * b); |
52 |
| - */ |
53 |
| - Console.ReadKey(); |
54 |
| - } |
| 49 | + using (Py.GIL()) |
| 50 | + { |
| 51 | + dynamic np = Py.Import("numpy"); |
| 52 | + dynamic sin = np.sin; |
| 53 | + Console.WriteLine(np.cos(np.pi*2)); |
| 54 | + Console.WriteLine(sin(5)); |
| 55 | + double c = np.cos(5) + sin(5); |
| 56 | + Console.WriteLine(c); |
| 57 | + /* this block is temporarily disabled due to regression |
| 58 | + dynamic a = np.array(new List<float> { 1, 2, 3 }); |
| 59 | + dynamic b = np.array(new List<float> { 6, 5, 4 }, Py.kw("dtype", np.int32)); |
| 60 | + Console.WriteLine(a.dtype); |
| 61 | + Console.WriteLine(b.dtype); |
| 62 | + Console.WriteLine(a * b); */ |
| 63 | + Console.ReadKey(); |
| 64 | + } |
55 | 65 | }
|
56 | 66 | ```
|
57 |
| -outputs: |
58 |
| -``` |
59 |
| -1.0 |
60 |
| --0.958924274663 |
61 |
| --0.6752620892 |
62 |
| -float64 |
63 |
| -int32 |
64 |
| -[ 6. 10. 12.] |
| 67 | + |
| 68 | +Output: |
| 69 | + |
| 70 | +```c |
| 71 | + 1.0 |
| 72 | + -0.958924274663 |
| 73 | + -0.6752620892 |
| 74 | + float64 |
| 75 | + int32 |
| 76 | + [6. 10. 12.] |
65 | 77 | ```
|
| 78 | + |
| 79 | +[travis shield]: https://travis-ci.org/pythonnet/pythonnet.png?branch=master |
| 80 | + |
| 81 | +[appveyor shield]: https://ci.appveyor.com/api/projects/status/g4flfwq46g2adv6a/branch/master?svg=true |
| 82 | + |
| 83 | +[license shield]: https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square |
0 commit comments