.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.1-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1db9edf41489c1227746cf6382bd087e08b494632010a6bead92984f000a4bf |
|
MD5 | fc274e099ecbbb421be0214a6f36048c |
|
BLAKE2b-256 | 45c3df81e58af5b1e327ee89bd6a6b2f07e349d604f8c67ff06aa2aad98aadbd |
Hashes for pythonnet-2.5.1-cp38-cp38-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 71d5af9205a6f872e654b21a4f77fb2385aa9feff8760057c0518dc470f19d6f |
|
MD5 | 729d510557978db2f8aed3af3e662b58 |
|
BLAKE2b-256 | 11e2e4ef3beab530e3c1dabf321b42f7b62da6b1f7a4722704b0a0727367c67a |
Hashes for pythonnet-2.5.1-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6bbf050084224d54da7aeb6a90f068e90ae4d3750248700aa0acab7d657df8cd |
|
MD5 | 3fd23fc2f6ce852957f59342f14909ce |
|
BLAKE2b-256 | 66cc92687c78c6c2ff15d312282bb2eead32a98e3a721d429e10f12d7d5573ad |
Hashes for pythonnet-2.5.1-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 93065007501edbe9a97dff1dbb041eeb8f3c1551d469070761b7a75c24308fa2 |
|
MD5 | d88d881ac40062c71ef653759c5e7a19 |
|
BLAKE2b-256 | 01b60b4b949a872afbfcc9b1cec15920a41a061c5889d2d1343d5b9a5b83f0c7 |
Hashes for pythonnet-2.5.1-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b808b3278395f2a2b166844d6b23032f28cda22e5296d2e57a8ecd38cb0d554 |
|
MD5 | 089fa4343f691967b710a02fa8baa3ff |
|
BLAKE2b-256 | 792107fe32571a63f9752a643883a795ab2b9ee8bcac1c0142b051c679c74e5c |
Hashes for pythonnet-2.5.1-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0cd76232072b15bba4459cbf9475d81348065ac120e20c301d2593621dd0a76 |
|
MD5 | f6c1aa61b4e3bbc3733c508b6bac7f72 |
|
BLAKE2b-256 | 9b588f0f8874407fdcfb56eb5ca02d6b4e5f13f494869ac08d23361534ae6282 |
Hashes for pythonnet-2.5.1-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | faa3c2a90aadce92bfb28570830b498954243eb5c6907d41e9dcbd936ec9ca52 |
|
MD5 | 40566a5715c3dc355c5ba5a26861606d |
|
BLAKE2b-256 | 65ade80fd5fc22aa7e6578fadee6f18bb9f92a4efcfda264bc556b42e4b358bb |
Hashes for pythonnet-2.5.1-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3555c3a1ed5e7359d156043e1b4a4db2c6ae90fad0e8c70547af01e76c86f374 |
|
MD5 | df768db32af213e6a821044ca1576a53 |
|
BLAKE2b-256 | 6d7d8127aa7facea268df4fddeab779fb3b36479eca3578375a4d416a7163c83 |
Hashes for pythonnet-2.5.1-cp27-cp27m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6fb41f0b9f3cd9a7ad581e6c817ad44e8ba7cd9ee4b91bbb7718fd85284b0c65 |
|
MD5 | a5d0bf50588156bc822e4ca64fa92cad |
|
BLAKE2b-256 | 531c1db3d9dfe3e6336d4132e1b123118baeb0124e64d0653f24bef2f7377b65 |
Hashes for pythonnet-2.5.1-cp27-cp27m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d1ec84c7d1ec9d31350383bb4832ef51a916e43cae11d7f7fc127985f62ab6c |
|
MD5 | 10730e3bd0a08da4024faa7b30aecaca |
|
BLAKE2b-256 | 1ed74fe0d9bf7d6b74fb03607901877b34ae51c461a3883c5228426236ab75a4 |