Skip to main content

.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) or Mono 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 or Mono application.

http://pythonnet.github.io/

Build Status

Build status

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.pi2 works, 2np.pi doesn’t

EG:

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();
}
}

outputs:

1.0
-0.958924274663
-0.6752620892
float64
int32
[ 6. 10. 12.]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pythonnet-2.2.0-dev1.tar.gz (732.6 kB view details)

Uploaded Source

Built Distributions

pythonnet-2.2.0.dev1-cp35-cp35m-win_amd64.whl (57.6 kB view details)

Uploaded CPython 3.5mWindows x86-64

pythonnet-2.2.0.dev1-cp35-cp35m-win32.whl (57.7 kB view details)

Uploaded CPython 3.5mWindows x86

pythonnet-2.2.0.dev1-cp34-cp34m-win_amd64.whl (57.6 kB view details)

Uploaded CPython 3.4mWindows x86-64

pythonnet-2.2.0.dev1-cp34-cp34m-win32.whl (57.6 kB view details)

Uploaded CPython 3.4mWindows x86

pythonnet-2.2.0.dev1-cp33-cp33m-win_amd64.whl (57.5 kB view details)

Uploaded CPython 3.3mWindows x86-64

pythonnet-2.2.0.dev1-cp33-cp33m-win32.whl (57.6 kB view details)

Uploaded CPython 3.3mWindows x86

pythonnet-2.2.0.dev1-cp27-cp27m-win_amd64.whl (56.6 kB view details)

Uploaded CPython 2.7mWindows x86-64

pythonnet-2.2.0.dev1-cp27-cp27m-win32.whl (56.6 kB view details)

Uploaded CPython 2.7mWindows x86

File details

Details for the file pythonnet-2.2.0-dev1.tar.gz.

File metadata

  • Download URL: pythonnet-2.2.0-dev1.tar.gz
  • Upload date:
  • Size: 732.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pythonnet-2.2.0-dev1.tar.gz
Algorithm Hash digest
SHA256 42ca0601a507e8fd3be89ea56597984950f1932f5cbd7ba558f5fc7ea436bb51
MD5 bd11b89e25c009ad584e074a0aab12b3
BLAKE2b-256 0ad998d1046bfe81a58caef405087c27f7a6e8113bdc4d41f10651d3b72f2cbf

See more details on using hashes here.

File details

Details for the file pythonnet-2.2.0.dev1-cp35-cp35m-win_amd64.whl.

File metadata

File hashes

Hashes for pythonnet-2.2.0.dev1-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 c7d65ba7965234c090aaec4b4111354b3333c2128a54c04ab6960d004c43b3a9
MD5 f44eda7d1f04ebe189d0d47ff16cbc25
BLAKE2b-256 7ecca2ad0c0c4da72145568acd9cef786671710ef7a41d50e8a6b05fd6cc8178

See more details on using hashes here.

File details

Details for the file pythonnet-2.2.0.dev1-cp35-cp35m-win32.whl.

File metadata

File hashes

Hashes for pythonnet-2.2.0.dev1-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 4d871f2b87c9749870171f02d336f4ab0456f2c1b59625a777581d75c4311436
MD5 cdd6e9752ad5e5e4065bec7d7fea927f
BLAKE2b-256 402bafea6fb2aa5b1e7170a4755df4814b6005d42067301f26c03c5a710b344b

See more details on using hashes here.

File details

Details for the file pythonnet-2.2.0.dev1-cp34-cp34m-win_amd64.whl.

File metadata

File hashes

Hashes for pythonnet-2.2.0.dev1-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 15754abe10113761cf02ad884bb6d58efc815f4c75d4b0941e860b6d036e40ec
MD5 8de9ef63e3f9e2f280db0334433cb21a
BLAKE2b-256 dc424089c86de3f7e345c59d65bca3ba2f570cc3c913b178ff2504e9bb489f73

See more details on using hashes here.

File details

Details for the file pythonnet-2.2.0.dev1-cp34-cp34m-win32.whl.

File metadata

File hashes

Hashes for pythonnet-2.2.0.dev1-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 e432a6e0807c0da9d0e13cdf330a36dac3fbc4277f19d18b6877df7991e25f12
MD5 3067ce64bdc09b8fff2cc4fbd8a3bbe2
BLAKE2b-256 2a3b87d931c22f30b25544e46b158bbbde9180fc4bb6d08c86802aaee157326f

See more details on using hashes here.

File details

Details for the file pythonnet-2.2.0.dev1-cp33-cp33m-win_amd64.whl.

File metadata

File hashes

Hashes for pythonnet-2.2.0.dev1-cp33-cp33m-win_amd64.whl
Algorithm Hash digest
SHA256 0c90f8e416f22193330b61a7e841dd6784125d6d6ded339979281fe5e9e8f6f8
MD5 c919524764474a36f56756c2470a5721
BLAKE2b-256 ced375ab244ff8c3148ebdd526fe51a3d2232cd4baa2967569c911cb30d29759

See more details on using hashes here.

File details

Details for the file pythonnet-2.2.0.dev1-cp33-cp33m-win32.whl.

File metadata

File hashes

Hashes for pythonnet-2.2.0.dev1-cp33-cp33m-win32.whl
Algorithm Hash digest
SHA256 a6d901406b138f5dedf19c39815c0b475a0646f9e7ff28c849e3f9247d1ef155
MD5 9f8ea2fb8424eb9950fa8d95cd9197e5
BLAKE2b-256 e90b3a0cb670d9ec6ce6dab7ec3c198bb3ad17f22f3486fef033f9ddcdb64add

See more details on using hashes here.

File details

Details for the file pythonnet-2.2.0.dev1-cp27-cp27m-win_amd64.whl.

File metadata

File hashes

Hashes for pythonnet-2.2.0.dev1-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 91c269051273fd3179b119772401393fe480233f3ceaf63df406192e55c9d3ab
MD5 4abbdfdfd074fa0d5d5f1c0c9ee0b9c8
BLAKE2b-256 23faa317737610ae8fc6444f3265479d40243f7ac00a69d99f5c244bf1ecbed3

See more details on using hashes here.

File details

Details for the file pythonnet-2.2.0.dev1-cp27-cp27m-win32.whl.

File metadata

File hashes

Hashes for pythonnet-2.2.0.dev1-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 06199ab34f2b5d0cfa0e40b2f0c24341b65b3f47d0bc1ad50e0d2e8c19715cb3
MD5 768bed9b39f85cacb9834e49c13a599b
BLAKE2b-256 09357c7db5baf1f0d3c220b52b82f7600dcd8e1057553d88517686ea920ae655

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page