.Net and Mono integration for Python
Project description
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.
Calling .NET code from Python
Python for .NET allows CLR namespaces to be treated essentially as Python packages.
import clr
from System import String
from System.Collections import *
To load an assembly, use the AddReference function in the clr module:
import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import Form
Embedding Python in .NET
All calls to python should be inside a using (Py.GIL()) {/* Your code here */} block.
Import python modules using dynamic mod = Py.Import("mod"), then you can call functions as normal, eg mod.func(args).
Use mod.func(args, Py.kw("keywordargname", keywordargvalue)) or mod.func(args, keywordargname: keywordargvalue) to apply keyword arguments.
All python objects should be declared as dynamic type.
Mathematical operations involving python and literal/managed types must have the python object first, eg. np.pi * 2 works, 2 * np.pi doesn’t.
Example
static void Main(string[] args)
{
using (Py.GIL())
{
dynamic np = Py.Import("numpy");
Console.WriteLine(np.cos(np.pi * 2));
dynamic sin = np.sin;
Console.WriteLine(sin(5));
double c = np.cos(5) + sin(5);
Console.WriteLine(c);
dynamic a = np.array(new List<float> { 1, 2, 3 });
Console.WriteLine(a.dtype);
dynamic b = np.array(new List<float> { 6, 5, 4 }, dtype: np.int32);
Console.WriteLine(b.dtype);
Console.WriteLine(a * b);
Console.ReadKey();
}
}
Output:
1.0
-0.958924274663
-0.6752620892
float64
int32
[ 6. 10. 12.]
Information on installation, FAQ, troubleshooting, debugging, and projects using pythonnet can be found in the Wiki:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for pythonnet-2.4.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6483dba7db4053da0cb4fdfa3580fed34623ac9100c3d0ce90e7688dc5d3ac47 |
|
MD5 | 28f53c8ae79f4106bdd78c7592f2ca93 |
|
BLAKE2b-256 | 65edcd200e95392d7f4a74319ff9166ba8a4a6fb28a405b065297f3fdec82da9 |
Hashes for pythonnet-2.4.0-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1b3f5d87996302557d876c582590438f71ffe6b7076e79865c6afd57adf476c |
|
MD5 | e2802db70013d3a7edebd41cdade376e |
|
BLAKE2b-256 | 9c3e8a2d13c7f7e8dbb6390587d69ffccdcc938fb464158c2288bcd5195a68d6 |
Hashes for pythonnet-2.4.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0bc4dfe352ab97ee6f00f13ff5b70ce5ba67576913a523cc4ec6e3fdaadf83fd |
|
MD5 | 3b44498ec166ce008c8f28110de38598 |
|
BLAKE2b-256 | c5a16f6b3b02449f17ec444c1f9d79e4831e895c35349abb7d5f7ead91440a63 |
Hashes for pythonnet-2.4.0-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38fdb7f5f2e1889175b7d88c64adc902e537d7d646cdc88ee7c0389bb1fdfd34 |
|
MD5 | 77b59dbd9ebca74fc82d26ab82cb657e |
|
BLAKE2b-256 | 80bd958beb743edc8af4c1289fbb9948fde2a193dcf728f6b4867a34c4e64f95 |
Hashes for pythonnet-2.4.0-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f16af543895fbaadfc593b9c436bf1a2c0e32329f80401aefc316a52e8f9bd18 |
|
MD5 | 85a62298d9c832ab6be0ede5070868c1 |
|
BLAKE2b-256 | ed1e5b2534f321a29e043dddc541d016de2d28bf1f12d2d8b6d288a3213443f9 |
Hashes for pythonnet-2.4.0-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7be226daafc2d40b30cb80a16e298460bd68f3484d8c7fce1ff82f4f35cda321 |
|
MD5 | 57313b18ea94dd4986f44f8a91be39ac |
|
BLAKE2b-256 | e5fafac0f0971bb6ca828c28f05065d16b4bd1978ae00304f91102c9620a6d20 |
Hashes for pythonnet-2.4.0-cp27-cp27m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1e380e7735b047258da469e36e122d1e0a13f79b4b4902b852a0dc626c0ceb0a |
|
MD5 | d434c0a2368efca2fade62e73fb78865 |
|
BLAKE2b-256 | 5c321d0adf14e854ec7440fe2310037ddd03391a2aae2f791fbdb26accf32726 |
Hashes for pythonnet-2.4.0-cp27-cp27m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 878e27a1a34ebf6b7a160abd1269d75a4fbebf5323e02679ed8226c0ad5fbf10 |
|
MD5 | d7c692a4e623ba5582b8f30d93a26755 |
|
BLAKE2b-256 | 76af29614b546e691679b8a433bd66760fb4b155eaab2ee02d6342643dacf878 |