.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) or Mono 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 or Mono 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)) 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.pi2 works, 2np.pi doesn’t
EG:
static void Main(string[] args)
{
using (Py.GIL()) {
dynamic np = Py.Import("numpy");
dynamic sin = np.sin;
Console.WriteLine(np.cos(np.pi*2));
Console.WriteLine(sin(5));
double c = np.cos(5) + sin(5);
Console.WriteLine(c);
/* this block is temporarily disabled due to regression
dynamic a = np.array(new List<float> { 1, 2, 3 });
dynamic b = np.array(new List<float> { 6, 5, 4 }, Py.kw("dtype", np.int32));
Console.WriteLine(a.dtype);
Console.WriteLine(b.dtype);
Console.WriteLine(a * b);
*/
Console.ReadKey();
}
}
outputs:
1.0 -0.958924274663 -0.6752620892 float64 int32 [ 6. 10. 12.]
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.2.0.dev1-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c7d65ba7965234c090aaec4b4111354b3333c2128a54c04ab6960d004c43b3a9 |
|
MD5 | f44eda7d1f04ebe189d0d47ff16cbc25 |
|
BLAKE2b-256 | 7ecca2ad0c0c4da72145568acd9cef786671710ef7a41d50e8a6b05fd6cc8178 |
Hashes for pythonnet-2.2.0.dev1-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4d871f2b87c9749870171f02d336f4ab0456f2c1b59625a777581d75c4311436 |
|
MD5 | cdd6e9752ad5e5e4065bec7d7fea927f |
|
BLAKE2b-256 | 402bafea6fb2aa5b1e7170a4755df4814b6005d42067301f26c03c5a710b344b |
Hashes for pythonnet-2.2.0.dev1-cp34-cp34m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15754abe10113761cf02ad884bb6d58efc815f4c75d4b0941e860b6d036e40ec |
|
MD5 | 8de9ef63e3f9e2f280db0334433cb21a |
|
BLAKE2b-256 | dc424089c86de3f7e345c59d65bca3ba2f570cc3c913b178ff2504e9bb489f73 |
Hashes for pythonnet-2.2.0.dev1-cp34-cp34m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e432a6e0807c0da9d0e13cdf330a36dac3fbc4277f19d18b6877df7991e25f12 |
|
MD5 | 3067ce64bdc09b8fff2cc4fbd8a3bbe2 |
|
BLAKE2b-256 | 2a3b87d931c22f30b25544e46b158bbbde9180fc4bb6d08c86802aaee157326f |
Hashes for pythonnet-2.2.0.dev1-cp33-cp33m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c90f8e416f22193330b61a7e841dd6784125d6d6ded339979281fe5e9e8f6f8 |
|
MD5 | c919524764474a36f56756c2470a5721 |
|
BLAKE2b-256 | ced375ab244ff8c3148ebdd526fe51a3d2232cd4baa2967569c911cb30d29759 |
Hashes for pythonnet-2.2.0.dev1-cp33-cp33m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a6d901406b138f5dedf19c39815c0b475a0646f9e7ff28c849e3f9247d1ef155 |
|
MD5 | 9f8ea2fb8424eb9950fa8d95cd9197e5 |
|
BLAKE2b-256 | e90b3a0cb670d9ec6ce6dab7ec3c198bb3ad17f22f3486fef033f9ddcdb64add |
Hashes for pythonnet-2.2.0.dev1-cp27-cp27m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 91c269051273fd3179b119772401393fe480233f3ceaf63df406192e55c9d3ab |
|
MD5 | 4abbdfdfd074fa0d5d5f1c0c9ee0b9c8 |
|
BLAKE2b-256 | 23faa317737610ae8fc6444f3265479d40243f7ac00a69d99f5c244bf1ecbed3 |
Hashes for pythonnet-2.2.0.dev1-cp27-cp27m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06199ab34f2b5d0cfa0e40b2f0c24341b65b3f47d0bc1ad50e0d2e8c19715cb3 |
|
MD5 | 768bed9b39f85cacb9834e49c13a599b |
|
BLAKE2b-256 | 09357c7db5baf1f0d3c220b52b82f7600dcd8e1057553d88517686ea920ae655 |