.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.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 796bd77a21c50414e8b15816b05bb66c90ea636786d7cf96ea77c534f274e898 |
|
MD5 | a59c535fac534b02fa904f89a9599580 |
|
BLAKE2b-256 | 3c5de44966ffb174bc8a7fe21d1e4d488997cc4bf4e81f55d442ba9c1f37cf8a |
Hashes for pythonnet-2.5.0-cp38-cp38-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | df7819c23deed9b2541e5f4fd91dd69d6f286d174f284080498ede31f9aea4a5 |
|
MD5 | f0a419a965484b89c9efec9bb1b5d0fa |
|
BLAKE2b-256 | c08bacebd9b13a23f3d3b432f6922f009f6317a3c093a4b6b521e932f6e73018 |
Hashes for pythonnet-2.5.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e8bb08c0058c5d42ecd47dc532b23d66089420ffe68ab4e75cea7820822b71f5 |
|
MD5 | 9fc01108fee7b74acb9db89b5422063a |
|
BLAKE2b-256 | 192c65404ef224a1bbec67beefcd659f694fcc06aa159efd72f50f59e2f677e8 |
Hashes for pythonnet-2.5.0-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0fff3addacaa82a26b1cebce8c8049dfebf7356e06cf4532074897f96a203495 |
|
MD5 | 1320144daa48c2007edb498d5cde6478 |
|
BLAKE2b-256 | 5db9302a18255b6b55d7f252bbe0bf29a9b2788e9ec860b3e7f779b3b03b9fb1 |
Hashes for pythonnet-2.5.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 585a170e8580528a3533944514895e462173067a5dfbb0784e5f49c45748ed83 |
|
MD5 | a75ff2f5889c0ba69ebcb1ba4aeae943 |
|
BLAKE2b-256 | 6a5da9bb0007337affddb7cd662d4ebde7b07e119a39f8a7010a55401efa77c1 |
Hashes for pythonnet-2.5.0-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5501b6016dec7b12918d79d6fb75fedd13eec54c90447e1a063727a0379b1204 |
|
MD5 | 81ac52a23cffe0e0d2a45e9eedbf07e9 |
|
BLAKE2b-256 | f7fc4240299cb33acab0a6730919d2c2204bf98a0a5d945b6c72f410ca69d707 |
Hashes for pythonnet-2.5.0-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0976a2a42a78c18e620d0626349e941e950d09e4591c13b3e00fd4effe4a9446 |
|
MD5 | e114ce8a0caa563c1fc7fcb541208d39 |
|
BLAKE2b-256 | ddbb3e0393c09907e9e25a55229d9f6dacc10d5335d1e41412cb6c253ab67e8e |
Hashes for pythonnet-2.5.0-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 628fb08d948856a5ed238750e7a02d32f999a94c28cfae0cf83afb5b2db02812 |
|
MD5 | 250800db7fb09faa3dc3fdca80bbd11e |
|
BLAKE2b-256 | ae037176dca0b7b3260129769f3f0ca4936e194539a6d8557fe02c7fe0cceba5 |
Hashes for pythonnet-2.5.0-cp27-cp27m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fcf0ee24d3a2a41ca1f5a74dfed8581c992c1bd3f0d36ac0a9ec9f4c0abba31f |
|
MD5 | f304a50b8fdf3aa49488ba165c6db029 |
|
BLAKE2b-256 | e5a370e7b75d1f953a1faa48a5e48d0a48f0ebf58b2a45d4d4e14dded233c9c3 |
Hashes for pythonnet-2.5.0-cp27-cp27m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c25605d2768c58de589fbe34a69f3dd91783d9e60d66bd543fd60effa224f3c1 |
|
MD5 | 5bcd96715e6c0be9037da2152a0e6d90 |
|
BLAKE2b-256 | 5914d9df8fed68df22e93d68ea6e946e6a6414a1ae28a992c86ec2c4c64033c1 |