pythonnet


Namepythonnet JSON
Version 3.0.2 PyPI version JSON
download
home_page
Summary.NET and Mono integration for Python
upload_time2023-08-29 15:27:54
maintainer
docs_urlNone
author
requires_python<3.12,>=3.7
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            pythonnet - Python.NET
===========================

|Join the chat at https://gitter.im/pythonnet/pythonnet| |stackexchange shield|

|gh shield|

|license shield|

|pypi package version| |conda-forge version| |python supported shield|

|nuget preview shield| |nuget release shield|

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.

.. code-block:: python

   import clr
   from System import String
   from System.Collections import *

To load an assembly, use the ``AddReference`` function in the ``clr``
module:

.. code-block:: python

   import clr
   clr.AddReference("System.Windows.Forms")
   from System.Windows.Forms import Form

By default, Mono will be used on Linux and macOS, .NET Framework on Windows. For
details on the loading of different runtimes, please refer to the documentation.

.NET Core
~~~~~~~~~

If .NET Core is installed in a default location or the ``dotnet`` CLI tool is on
the ``PATH``, loading it instead of the default (Mono/.NET Framework) runtime
just requires setting either the environment variable
``PYTHONNET_RUNTIME=coreclr`` or calling ``pythonnet.load`` explicitly:

.. code-block:: python

   from pythonnet import load
   load("coreclr")

   import clr


Embedding Python in .NET
------------------------

-  You must set ``Runtime.PythonDLL`` property or ``PYTHONNET_PYDLL`` environment variable
   starting with version 3.0, otherwise you will receive ``BadPythonDllException``
   (internal, derived from ``MissingMethodException``) upon calling ``Initialize``.
   Typical values are ``python38.dll`` (Windows), ``libpython3.8.dylib`` (Mac),
   ``libpython3.8.so`` (most other Unix-like operating systems).
-  Then call ``PythonEngine.Initialize()``. If you plan to use Python objects from
   multiple threads, also call ``PythonEngine.BeginAllowThreads()``.
-  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
~~~~~~~

.. code-block:: csharp

   static void Main(string[] args)
   {
       PythonEngine.Initialize();
       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 = (double)(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:

.. code:: csharp

   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:

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

Mailing list
    https://mail.python.org/mailman/listinfo/pythondotnet
Chat
    https://gitter.im/pythonnet/pythonnet

.NET Foundation
---------------
This project is supported by the `.NET Foundation <https://dotnetfoundation.org>`_.

.. |Join the chat at https://gitter.im/pythonnet/pythonnet| image:: https://badges.gitter.im/pythonnet/pythonnet.svg
   :target: https://gitter.im/pythonnet/pythonnet?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
.. |license shield| image:: https://img.shields.io/badge/license-MIT-blue.svg?maxAge=3600
   :target: ./LICENSE
.. |pypi package version| image:: https://img.shields.io/pypi/v/pythonnet.svg
   :target: https://pypi.python.org/pypi/pythonnet
.. |python supported shield| image:: https://img.shields.io/pypi/pyversions/pythonnet.svg
   :target: https://pypi.python.org/pypi/pythonnet
.. |stackexchange shield| image:: https://img.shields.io/badge/StackOverflow-python.net-blue.svg
   :target: http://stackoverflow.com/questions/tagged/python.net
.. |conda-forge version| image:: https://img.shields.io/conda/vn/conda-forge/pythonnet.svg
   :target: https://anaconda.org/conda-forge/pythonnet
.. |nuget preview shield| image:: https://img.shields.io/nuget/vpre/pythonnet
   :target: https://www.nuget.org/packages/pythonnet/
.. |nuget release shield| image:: https://img.shields.io/nuget/v/pythonnet
   :target: https://www.nuget.org/packages/pythonnet/
.. |gh shield| image:: https://github.com/pythonnet/pythonnet/workflows/GitHub%20Actions/badge.svg
   :target: https://github.com/pythonnet/pythonnet/actions?query=branch%3Amaster

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "pythonnet",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "<3.12,>=3.7",
    "maintainer_email": "",
    "keywords": "",
    "author": "",
    "author_email": "\"The Contributors of the Python.NET Project\" <pythonnet@python.org>",
    "download_url": "https://files.pythonhosted.org/packages/00/e5/d54c65f4e4435c117ae0a305d4684c954211bed9b7bebc6654ac92b7e818/pythonnet-3.0.2.tar.gz",
    "platform": null,
    "description": "pythonnet - Python.NET\n===========================\n\n|Join the chat at https://gitter.im/pythonnet/pythonnet| |stackexchange shield|\n\n|gh shield|\n\n|license shield|\n\n|pypi package version| |conda-forge version| |python supported shield|\n\n|nuget preview shield| |nuget release shield|\n\nPython.NET is a package that gives Python programmers nearly\nseamless integration with the .NET Common Language Runtime (CLR) and\nprovides a powerful application scripting tool for .NET developers. It\nallows Python code to interact with the CLR, and may also be used to\nembed Python into a .NET application.\n\nCalling .NET code from Python\n-----------------------------\n\nPython.NET allows CLR namespaces to be treated essentially as Python packages.\n\n.. code-block:: python\n\n   import clr\n   from System import String\n   from System.Collections import *\n\nTo load an assembly, use the ``AddReference`` function in the ``clr``\nmodule:\n\n.. code-block:: python\n\n   import clr\n   clr.AddReference(\"System.Windows.Forms\")\n   from System.Windows.Forms import Form\n\nBy default, Mono will be used on Linux and macOS, .NET Framework on Windows. For\ndetails on the loading of different runtimes, please refer to the documentation.\n\n.NET Core\n~~~~~~~~~\n\nIf .NET Core is installed in a default location or the ``dotnet`` CLI tool is on\nthe ``PATH``, loading it instead of the default (Mono/.NET Framework) runtime\njust requires setting either the environment variable\n``PYTHONNET_RUNTIME=coreclr`` or calling ``pythonnet.load`` explicitly:\n\n.. code-block:: python\n\n   from pythonnet import load\n   load(\"coreclr\")\n\n   import clr\n\n\nEmbedding Python in .NET\n------------------------\n\n-  You must set ``Runtime.PythonDLL`` property or ``PYTHONNET_PYDLL`` environment variable\n   starting with version 3.0, otherwise you will receive ``BadPythonDllException``\n   (internal, derived from ``MissingMethodException``) upon calling ``Initialize``.\n   Typical values are ``python38.dll`` (Windows), ``libpython3.8.dylib`` (Mac),\n   ``libpython3.8.so`` (most other Unix-like operating systems).\n-  Then call ``PythonEngine.Initialize()``. If you plan to use Python objects from\n   multiple threads, also call ``PythonEngine.BeginAllowThreads()``.\n-  All calls to python should be inside a\n   ``using (Py.GIL()) {/* Your code here */}`` block.\n-  Import python modules using ``dynamic mod = Py.Import(\"mod\")``, then\n   you can call functions as normal, eg ``mod.func(args)``.\n-  Use ``mod.func(args, Py.kw(\"keywordargname\", keywordargvalue))`` or\n   ``mod.func(args, keywordargname: keywordargvalue)`` to apply keyword\n   arguments.\n-  All python objects should be declared as ``dynamic`` type.\n-  Mathematical operations involving python and literal/managed types\n   must have the python object first, eg. ``np.pi * 2`` works,\n   ``2 * np.pi`` doesn't.\n\nExample\n~~~~~~~\n\n.. code-block:: csharp\n\n   static void Main(string[] args)\n   {\n       PythonEngine.Initialize();\n       using (Py.GIL())\n       {\n           dynamic np = Py.Import(\"numpy\");\n           Console.WriteLine(np.cos(np.pi * 2));\n\n           dynamic sin = np.sin;\n           Console.WriteLine(sin(5));\n\n           double c = (double)(np.cos(5) + sin(5));\n           Console.WriteLine(c);\n\n           dynamic a = np.array(new List<float> { 1, 2, 3 });\n           Console.WriteLine(a.dtype);\n\n           dynamic b = np.array(new List<float> { 6, 5, 4 }, dtype: np.int32);\n           Console.WriteLine(b.dtype);\n\n           Console.WriteLine(a * b);\n           Console.ReadKey();\n       }\n   }\n\nOutput:\n\n.. code:: csharp\n\n   1.0\n   -0.958924274663\n   -0.6752620892\n   float64\n   int32\n   [  6.  10.  12.]\n\n\n\nResources\n---------\n\nInformation on installation, FAQ, troubleshooting, debugging, and\nprojects using pythonnet can be found in the Wiki:\n\nhttps://github.com/pythonnet/pythonnet/wiki\n\nMailing list\n    https://mail.python.org/mailman/listinfo/pythondotnet\nChat\n    https://gitter.im/pythonnet/pythonnet\n\n.NET Foundation\n---------------\nThis project is supported by the `.NET Foundation <https://dotnetfoundation.org>`_.\n\n.. |Join the chat at https://gitter.im/pythonnet/pythonnet| image:: https://badges.gitter.im/pythonnet/pythonnet.svg\n   :target: https://gitter.im/pythonnet/pythonnet?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge\n.. |license shield| image:: https://img.shields.io/badge/license-MIT-blue.svg?maxAge=3600\n   :target: ./LICENSE\n.. |pypi package version| image:: https://img.shields.io/pypi/v/pythonnet.svg\n   :target: https://pypi.python.org/pypi/pythonnet\n.. |python supported shield| image:: https://img.shields.io/pypi/pyversions/pythonnet.svg\n   :target: https://pypi.python.org/pypi/pythonnet\n.. |stackexchange shield| image:: https://img.shields.io/badge/StackOverflow-python.net-blue.svg\n   :target: http://stackoverflow.com/questions/tagged/python.net\n.. |conda-forge version| image:: https://img.shields.io/conda/vn/conda-forge/pythonnet.svg\n   :target: https://anaconda.org/conda-forge/pythonnet\n.. |nuget preview shield| image:: https://img.shields.io/nuget/vpre/pythonnet\n   :target: https://www.nuget.org/packages/pythonnet/\n.. |nuget release shield| image:: https://img.shields.io/nuget/v/pythonnet\n   :target: https://www.nuget.org/packages/pythonnet/\n.. |gh shield| image:: https://github.com/pythonnet/pythonnet/workflows/GitHub%20Actions/badge.svg\n   :target: https://github.com/pythonnet/pythonnet/actions?query=branch%3Amaster\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": ".NET and Mono integration for Python",
    "version": "3.0.2",
    "project_urls": {
        "Homepage": "https://pythonnet.github.io/",
        "Sources": "https://github.com/pythonnet/pythonnet"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4fe969401c5b4699560d37dadafe4bfb6af64d7cc8d8f16cb66c2bab99d59bb4",
                "md5": "b4f46a36667da7198ad97051bf9bf7d6",
                "sha256": "e048c5b3eba9bea56a1e28053956dcc566b77dab69caa73a641f881812712b64"
            },
            "downloads": -1,
            "filename": "pythonnet-3.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b4f46a36667da7198ad97051bf9bf7d6",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<3.12,>=3.7",
            "size": 285481,
            "upload_time": "2023-08-29T15:27:52",
            "upload_time_iso_8601": "2023-08-29T15:27:52.547126Z",
            "url": "https://files.pythonhosted.org/packages/4f/e9/69401c5b4699560d37dadafe4bfb6af64d7cc8d8f16cb66c2bab99d59bb4/pythonnet-3.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "00e5d54c65f4e4435c117ae0a305d4684c954211bed9b7bebc6654ac92b7e818",
                "md5": "3c8e3253e6f7ec6fba3689804882a7e2",
                "sha256": "2cdd1ccedc64a7c9bd711be3d0fd0c4a78932474139dc54aa697bedde741c746"
            },
            "downloads": -1,
            "filename": "pythonnet-3.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "3c8e3253e6f7ec6fba3689804882a7e2",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<3.12,>=3.7",
            "size": 426642,
            "upload_time": "2023-08-29T15:27:54",
            "upload_time_iso_8601": "2023-08-29T15:27:54.476762Z",
            "url": "https://files.pythonhosted.org/packages/00/e5/d54c65f4e4435c117ae0a305d4684c954211bed9b7bebc6654ac92b7e818/pythonnet-3.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-29 15:27:54",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "pythonnet",
    "github_project": "pythonnet",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "pythonnet"
}
        
Elapsed time: 0.16119s