.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)) 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");
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();
}
}
Output:
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.1-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c65865369c6c983eedac4aca3e12e4b4bf392e9cafc53035a8d1e36d69772c78 |
|
MD5 | 5c6d50cb5c1310fbc85fb541b776d586 |
|
BLAKE2b-256 | bc293537cfbd6bee460a7712ae8e4bac1b2b653a761496a53f9b7caf75106a8f |
Hashes for pythonnet-2.2.1-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c5f0c22c558be4099c7601230405cd29ce8f29de81da7428f96210e3e105367a |
|
MD5 | 2957b8c8988145385bcf7045a9a3e5ef |
|
BLAKE2b-256 | 4e04b462c45c77b233eb57ad87bd7f68c7964d54481c40e37749e8233455ad26 |
Hashes for pythonnet-2.2.1-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01e4e35fe2709254ce2efc0430e95b81871c86e4863f56c382bf234a7b8a7fa8 |
|
MD5 | 4d271650679ac92d2f3dc3ab6435103c |
|
BLAKE2b-256 | 100e7f0d1055f99f74c5346d86caea5a554080640c9ac1c8a79ea6af75f4a71c |
Hashes for pythonnet-2.2.1-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a153935f000ce9c1965b068fb28306e6746eb5d857ad62f4f199848da46de7d |
|
MD5 | 7b1174c9c0481c27b2573732d3ede869 |
|
BLAKE2b-256 | 87f0caa7681aeba98f940b76d76b053c4ca779d07a963f071f4da9d6832b35ee |
Hashes for pythonnet-2.2.1-cp34-cp34m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f6adaf1f96075152f20887b2b6eff52548ed8c4907a11674571a4c586f05929 |
|
MD5 | c9b087527e3903bb9fc1085686773ed5 |
|
BLAKE2b-256 | 5708f068cbe843cc5d9abacef25eca3644ad4ee81dd56eb72ff54a3dad34396a |
Hashes for pythonnet-2.2.1-cp34-cp34m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 526210012879f20a9dd88fe4b7cf0963a04803cd805ef00c9abf00190e74ca55 |
|
MD5 | 51b8915c2a2d5b4a13d395621af47dfc |
|
BLAKE2b-256 | ad4a092d4e535c6c9cddc5db0788b408f5803ee86961518493dcb4259c413975 |
Hashes for pythonnet-2.2.1-cp33-cp33m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f1d2d88af427ca0583a4a85fad8ccaf8e8f79c41268063640802bfc7ce2655e7 |
|
MD5 | b22c065d27bdc1d3ce0f364b411d3e8c |
|
BLAKE2b-256 | 45267fe0ddab2491f47ba84edf2fe6f5ede908914bdf626fec71bfa3c6249e4a |
Hashes for pythonnet-2.2.1-cp33-cp33m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 18ae617d157d110a14251d5242a82cd60a15dc1b65b6728cacd98c4e2f3f321d |
|
MD5 | 200368005cea19eb52bb88c61ec00ce1 |
|
BLAKE2b-256 | 9f2c3cad4e2311747b5531d911537feba7445cf1cf011f48adcca455000803dd |
Hashes for pythonnet-2.2.1-cp27-cp27m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f3ec98c8d0c1ca5d6ce9cb9cead3672909af22887c4b1fca6433ed7494c3241a |
|
MD5 | fcf2b1b31aec0e619b3ed4b955346fb8 |
|
BLAKE2b-256 | 8dc7904bed7321f07ff46b57dcbfd0d71abc6eae96eae08836a9f321bacbbfc9 |
Hashes for pythonnet-2.2.1-cp27-cp27m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea34ea84a5954121d29519d1bc90b5721743cda3c70c1a37c4543434a93d25e4 |
|
MD5 | fea125ea915bea6fcbaac2b688d2b04c |
|
BLAKE2b-256 | 056b90cfec03ba43e30598404e94c27b9efcb52930973b293dd9df4026ab86be |