Skip to main content

.Net and Mono integration for Python

Project description

Join the chat at https://gitter.im/pythonnet/pythonnet

appveyor shield travis shield codecov shield

license shield pypi package version python supported shield stackexchange shield

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)) 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.]

Information on installation, FAQ, troubleshooting, debugging, and projects using pythonnet can be found in the Wiki:

https://github.com/pythonnet/pythonnet/wiki

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.4.0rc2.tar.gz (1.8 MB view details)

Uploaded Source

Built Distributions

pythonnet-2.4.0rc2-cp37-cp37m-win_amd64.whl (71.2 kB view details)

Uploaded CPython 3.7mWindows x86-64

pythonnet-2.4.0rc2-cp37-cp37m-win32.whl (71.2 kB view details)

Uploaded CPython 3.7mWindows x86

pythonnet-2.4.0rc2-cp36-cp36m-win_amd64.whl (71.2 kB view details)

Uploaded CPython 3.6mWindows x86-64

pythonnet-2.4.0rc2-cp36-cp36m-win32.whl (71.2 kB view details)

Uploaded CPython 3.6mWindows x86

pythonnet-2.4.0rc2-cp35-cp35m-win_amd64.whl (71.2 kB view details)

Uploaded CPython 3.5mWindows x86-64

pythonnet-2.4.0rc2-cp35-cp35m-win32.whl (71.2 kB view details)

Uploaded CPython 3.5mWindows x86

pythonnet-2.4.0rc2-cp27-cp27m-win_amd64.whl (70.8 kB view details)

Uploaded CPython 2.7mWindows x86-64

pythonnet-2.4.0rc2-cp27-cp27m-win32.whl (70.8 kB view details)

Uploaded CPython 2.7mWindows x86

File details

Details for the file pythonnet-2.4.0rc2.tar.gz.

File metadata

  • Download URL: pythonnet-2.4.0rc2.tar.gz
  • Upload date:
  • Size: 1.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for pythonnet-2.4.0rc2.tar.gz
Algorithm Hash digest
SHA256 a1d6234a056bcc0f6d35a6fedf62ab867a7548ed166b976fcdaf38fb9ec252bf
MD5 0054a2b851d00d563fe3e7e0dee8e426
BLAKE2b-256 ef9527ad1e3a52f8ea60b4a6bceeb56c57a89c47ada370b69467c15e476abb56

See more details on using hashes here.

File details

Details for the file pythonnet-2.4.0rc2-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: pythonnet-2.4.0rc2-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 71.2 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for pythonnet-2.4.0rc2-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 d21afbc8aa91bd8bc217850039a5c66934a84bb853c385a0be077228c5fee24a
MD5 0dfdebaddbcb41fbc0b1ff91be62d538
BLAKE2b-256 65d60843db01800b829e634a8ea4f3c3c0aa5895a78d5ad51bb44fda90561bed

See more details on using hashes here.

File details

Details for the file pythonnet-2.4.0rc2-cp37-cp37m-win32.whl.

File metadata

  • Download URL: pythonnet-2.4.0rc2-cp37-cp37m-win32.whl
  • Upload date:
  • Size: 71.2 kB
  • Tags: CPython 3.7m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for pythonnet-2.4.0rc2-cp37-cp37m-win32.whl
Algorithm Hash digest
SHA256 a8495cd2b59ce6dffbda6799adb1713ebc69e5432d1117eca8002ba0c48894f5
MD5 e678745172a84e95f5794f285a3c9873
BLAKE2b-256 f923efbb4dd0641f922e2ee30ce390a4b77b0835b7bc02d70281e8928964cd8f

See more details on using hashes here.

File details

Details for the file pythonnet-2.4.0rc2-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: pythonnet-2.4.0rc2-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 71.2 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for pythonnet-2.4.0rc2-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 a781ce14c04775cf6e7ebc004d31e9cecf7a6aa52a4849efdbf09b9048ae6dcd
MD5 660a347136dda0db6c7a7ab46bdc39ae
BLAKE2b-256 1e7bf95a07ba8fa9e43de2f26d161451d9184ef767f8248beffdf73cc1e03f6c

See more details on using hashes here.

File details

Details for the file pythonnet-2.4.0rc2-cp36-cp36m-win32.whl.

File metadata

  • Download URL: pythonnet-2.4.0rc2-cp36-cp36m-win32.whl
  • Upload date:
  • Size: 71.2 kB
  • Tags: CPython 3.6m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for pythonnet-2.4.0rc2-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 b1abcbaf994b1bf88402fd786840e1055c57311ecb7d8fc0f4e52ae88bef713a
MD5 1dcf7bcfd1c518451d7f924bd4d3c4b3
BLAKE2b-256 94d731f9c00c5356da856d61890dfad78a82b906039662ba8894b02a0c722fbe

See more details on using hashes here.

File details

Details for the file pythonnet-2.4.0rc2-cp35-cp35m-win_amd64.whl.

File metadata

  • Download URL: pythonnet-2.4.0rc2-cp35-cp35m-win_amd64.whl
  • Upload date:
  • Size: 71.2 kB
  • Tags: CPython 3.5m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for pythonnet-2.4.0rc2-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 a54ffa85c0efd5ba57b13cc4736af19ee1711a74f73e05bae75d1569b50e0117
MD5 12e9bd5c3fffbdcca3715f9404153acd
BLAKE2b-256 08c6a4f3a41f4ab5c76a96edee24750504f28dea6e8bee03313e90b02f729ea1

See more details on using hashes here.

File details

Details for the file pythonnet-2.4.0rc2-cp35-cp35m-win32.whl.

File metadata

  • Download URL: pythonnet-2.4.0rc2-cp35-cp35m-win32.whl
  • Upload date:
  • Size: 71.2 kB
  • Tags: CPython 3.5m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for pythonnet-2.4.0rc2-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 52886e820ed2f6ae60762e8e378b9b41fb83a5beeb81c5c1e6b803eb59004c9f
MD5 a7a69dee2cb54cd7599e870eb0a91aad
BLAKE2b-256 42825e46ae63484bf1f5e49a8d74d31d67c5959019f36bd89f6a141008c669aa

See more details on using hashes here.

File details

Details for the file pythonnet-2.4.0rc2-cp27-cp27m-win_amd64.whl.

File metadata

  • Download URL: pythonnet-2.4.0rc2-cp27-cp27m-win_amd64.whl
  • Upload date:
  • Size: 70.8 kB
  • Tags: CPython 2.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for pythonnet-2.4.0rc2-cp27-cp27m-win_amd64.whl
Algorithm Hash digest
SHA256 2acfe12463b98c9fd1f760d44895caa725d107107de2de9d64246958eb7cbbe4
MD5 51d2b2db6876625df7765b0221a72410
BLAKE2b-256 f0c01d449e3af72081dab39bb7931e2d74f09c8094c53746c2a93a56fa87f2f6

See more details on using hashes here.

File details

Details for the file pythonnet-2.4.0rc2-cp27-cp27m-win32.whl.

File metadata

  • Download URL: pythonnet-2.4.0rc2-cp27-cp27m-win32.whl
  • Upload date:
  • Size: 70.8 kB
  • Tags: CPython 2.7m, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for pythonnet-2.4.0rc2-cp27-cp27m-win32.whl
Algorithm Hash digest
SHA256 3e490ee8199c7c97d624cf145de3dd5588bece2a456920ff9f79f74e12e31998
MD5 b61738927410df0fbb372a1861473945
BLAKE2b-256 1786d6616347e3adacf88f010df9ac5aec4fc3e88d3ad7d40fbb7fc3f3894dc8

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