.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.2-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00a4fed9fc05b4efbe8947c79dc0799cffbca4c89e3e068e70b6618f20c906f2 |
|
MD5 | e8e817122a24b999ab8c18aa6d792c9a |
|
BLAKE2b-256 | 71ec3480ddb32c76376596adc66a92a398e1cecd8f58bde784a624166ba534e6 |
Hashes for pythonnet-2.5.2-cp38-cp38-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 41a607b7304e9efc6d4d8db438d6018a17c6637e8b8998848ff5c2a7a1b4687c |
|
MD5 | 5b27978b1397dc628fbe828fabcd2dd4 |
|
BLAKE2b-256 | e2e3ae95ae2f13a453c373274c9e4a53b8e07f5dcf7d086c62004ae343f9f144 |
Hashes for pythonnet-2.5.2-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80c8f5c9bd10440a73eb6aedbbacb2f3dd7701b474816f5ef7636f529d838d38 |
|
MD5 | ec052f70093a0ded648a1fcb6fb57acd |
|
BLAKE2b-256 | 63bff39afc5a487399dfe72985d793e3c5726a6bc80ebb12561b696169fcac15 |
Hashes for pythonnet-2.5.2-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc77fc63e2afb0a80199ab44ced4fdfb78c19d8030063c345c80740d15380dd9 |
|
MD5 | 096fce23731fb06df8567375676a3d44 |
|
BLAKE2b-256 | b9fb7cd9cf8ae8ecccec0c048223e32a569a74b9cfc43ce28288d481ee709f48 |
Hashes for pythonnet-2.5.2-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 058e536062d1585d07ec5f2cf16aefcfc8eb8179faa90e5db0063d358469d025 |
|
MD5 | 67a436002882cc82e5cf02e2d4e884c8 |
|
BLAKE2b-256 | 0cb3e136267c9299ac5572286ccb807f4b0a6bad9b882e7ca94691004a6acc83 |
Hashes for pythonnet-2.5.2-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62645c29840c4a877d66e047f3e065b2e5a1a66431a99bce8d42a5af3a093ee1 |
|
MD5 | 98aff6c82930a74db031652671c66a66 |
|
BLAKE2b-256 | 2619978dd055028cad9e9a0b32dad8818fc6a7072954e7ad84f2b83fc1fac619 |
Hashes for pythonnet-2.5.2-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8e5b27de1e2cfb69b88782ac5cdf605b1a73598a85d86570e46961126628dbb |
|
MD5 | dc8c7e54618f829da770fa53d11181b0 |
|
BLAKE2b-256 | 9d8dad965cd823c781234b69db1caf9fa9d1cac93e9804bbf6dcc0eefcb79ac5 |
Hashes for pythonnet-2.5.2-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 840bdef89b378663d73f74f18895b6d8630d1f5671457a1db5ffb68179d85582 |
|
MD5 | eaab7adf9cf780b778f45cad32c5f196 |
|
BLAKE2b-256 | cc7f6259591d9aac8d31f4c2e08d23545921fdec6a80af69f11bed5382359fbe |
Hashes for pythonnet-2.5.2-cp27-cp27m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c02f53d0e61b202cddf3198fac9553d5b4ee0ea0cc4fe658c2ed69ab24def276 |
|
MD5 | 2ee3b06a5935e25ef8b1234e660ad0d5 |
|
BLAKE2b-256 | 86e72489af5b120d8ae764af66794527efd941d749d64401a6828eb36117ee5e |
Hashes for pythonnet-2.5.2-cp27-cp27m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d519bbc7b1cd3999651efc594d91cb67c46d1d8466dad3d83b578102e58d05bd |
|
MD5 | 385e3b9655b3e5b5642d16aa0431072a |
|
BLAKE2b-256 | eaa6ca8ce58d6bde0f55ab0627ae257d0c8a51769e7c1e91dc680a349193b8b2 |