Skip to main content
Warning: You are using the test version of PyPI. This is a pre-production deployment of Warehouse. Changes made here affect the production instance of TestPyPI (testpypi.python.org).
Help us improve Python packaging - Donate today!

.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) 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 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.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);
        /* this block is temporarily disabled due to regression #249
        dynamic a = np.array(new List<float> { 1, 2, 3 });
        Console.WriteLine(a.dtype);

        dynamic b = np.array(new List<float> { 6, 5, 4 }, Py.kw("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.]
Release History

Release History

This version
History Node

2.3.0

History Node

2.3.0.dev1

History Node

2.2.2

History Node

2.2.1

History Node

2.2.0

History Node

2.0.0.dev1

History Node

0.0.0

Download Files

Download Files

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

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
pythonnet-2.3.0-cp27-cp27m-win32.whl (58.8 kB) Copy SHA256 Checksum SHA256 cp27 Wheel Mar 11, 2017
pythonnet-2.3.0-cp27-cp27m-win_amd64.whl (58.8 kB) Copy SHA256 Checksum SHA256 cp27 Wheel Mar 11, 2017
pythonnet-2.3.0-cp33-cp33m-win32.whl (59.4 kB) Copy SHA256 Checksum SHA256 cp33 Wheel Mar 11, 2017
pythonnet-2.3.0-cp33-cp33m-win_amd64.whl (59.3 kB) Copy SHA256 Checksum SHA256 cp33 Wheel Mar 11, 2017
pythonnet-2.3.0-cp34-cp34m-win32.whl (59.4 kB) Copy SHA256 Checksum SHA256 cp34 Wheel Mar 11, 2017
pythonnet-2.3.0-cp34-cp34m-win_amd64.whl (59.3 kB) Copy SHA256 Checksum SHA256 cp34 Wheel Mar 11, 2017
pythonnet-2.3.0-cp35-cp35m-win32.whl (59.4 kB) Copy SHA256 Checksum SHA256 cp35 Wheel Mar 11, 2017
pythonnet-2.3.0.tar.gz (1.5 MB) Copy SHA256 Checksum SHA256 Source Mar 11, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting