.Net and Mono integration for Python
Project description
Python.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.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.]
Resources
Information on installation, FAQ, troubleshooting, debugging, and projects using pythonnet can be found in the Wiki:
.NET Foundation
This project is supported by the .NET Foundation.
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.5.0rc2-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 146fcd789eeb426bc3e37a4b810e4724ff17653d48ad7d7f2b6c1fa841fb2b0c |
|
MD5 | 72d1d55289da1fe113303c3f234a8b6b |
|
BLAKE2b-256 | d438d65a548c350b6e536e8dd1585bd57b90d8f843156fc0d99d4b6ef9dc5477 |
Hashes for pythonnet-2.5.0rc2-cp38-cp38-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dabfc89a70f91d8b47940f537a86cc81bc1e7367e62bfb83b21cc72d90a42b15 |
|
MD5 | 44537026f05e08d7d58ae37205cb7d48 |
|
BLAKE2b-256 | 0161241ac10d1b2f46251ec64b5be0b9f7f410ddc7f45940c5d2349dc8ad7f4c |
Hashes for pythonnet-2.5.0rc2-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 386b70f12bd5acd2f9af8878ec01c210d394509c478d401f9bd20fdf00105ed4 |
|
MD5 | 28109dc241dd433670f52419a5e5ba29 |
|
BLAKE2b-256 | e81789d65be08bc7ca738c9efd184f94e9446b625358b9afed37ffd411e1f8e5 |
Hashes for pythonnet-2.5.0rc2-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0a190e51d2b614a1ca3bb3af0953a538e3e06a2ca408dbd4bb283627f4b3610 |
|
MD5 | 7df11ac71591b61ef53addc29f58197b |
|
BLAKE2b-256 | 4846964fc74497c9d9753dcf4d092661efdba90eb2df10cd09a487af47601271 |
Hashes for pythonnet-2.5.0rc2-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a41eefe1a9b3cdd00f7d159c8a2afbab550c13fb7ebc6f0902fcf8ed278a85a |
|
MD5 | 731c30ef32b04ca75270426b3032f9b0 |
|
BLAKE2b-256 | edddcc8c5fefd1782a0887a6d882b3ca9d8dad20790eaa62b459015189a9195f |
Hashes for pythonnet-2.5.0rc2-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8cdedd387c5de9477fd123af9a28b28928ece729ef385da1b2fbad01e0f39d64 |
|
MD5 | 63cc9814acf087bf88e7216f218085fa |
|
BLAKE2b-256 | d50dcf1a7b23e62a6c5f6b3ccf93225874eaad114e0354faca920c2872aad28f |
Hashes for pythonnet-2.5.0rc2-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ffb548eed21267a4308b114736a341bf491a1d9ea9c45202fab898b3ac5601d9 |
|
MD5 | ef77c29355b50fe728525de06a8a5a21 |
|
BLAKE2b-256 | 4a40ae3ce51976802a7519b0e4c7cf18a41a3ab675435547e72fabc45606ed03 |
Hashes for pythonnet-2.5.0rc2-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f864de275927b1c4738c21fc6987e04f7d0df3ff6aad893d65a73bbbdcfb9e1 |
|
MD5 | 658cd4ff7a3da3687dd106691a61a875 |
|
BLAKE2b-256 | 1edd3eebd89ae356f3496c1338e0dc1e94f92a396dbc0ac4c953c6fb137e5835 |
Hashes for pythonnet-2.5.0rc2-cp27-cp27m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a45da12e014d8611b85aecb7236ed88e3c2dfcc6531104a15f54bf0a2d58d81 |
|
MD5 | 30639ae768ec24bbbe9b5b32263c1958 |
|
BLAKE2b-256 | 5d4afa10e78cab0c5a161bc77e6c11ea6fa1482639a03deb3eceaebc73b3bc50 |
Hashes for pythonnet-2.5.0rc2-cp27-cp27m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a81cec127a3870bc588dc75c71db71e25068364478da07db11e8867c052efbee |
|
MD5 | 326337cc3664d2a28575aa7049703039 |
|
BLAKE2b-256 | 0f2051a780e29d746b667201759579a7e48f255dd042fc511586608f200eab9b |