{"name":"pythonnet: Seamless Python and .NET Integration","description":"pythonnet offers Python programmers seamless integration with the .NET Common Language Runtime (CLR), enabling powerful application scripting for .NET developers. It allows Python code to interact directly with the CLR and can also be used to embed Python within .NET applications.","github":"https://github.com/pythonnet/pythonnet","url":"https://osrepos.com/repo/pythonnet-pythonnet","source":"osrepos.com","sourceDescription":"This repository profile is provided by osrepos.com, an open source repository discovery platform.","repositoryProfile":"https://osrepos.com/repo/pythonnet-pythonnet","generatedFor":"open source discovery and AI-assisted research","markdown":"https://osrepos.com/repo/pythonnet-pythonnet.md","json":"https://osrepos.com/repo/pythonnet-pythonnet.json","topics":["pythonnet","python","dotnet","csharp","clr","interoperability","ffi","scripting"],"keywords":["pythonnet","python","dotnet","csharp","clr","interoperability","ffi","scripting"],"stars":null,"summary":"pythonnet offers Python programmers seamless integration with the .NET Common Language Runtime (CLR), enabling powerful application scripting for .NET developers. It allows Python code to interact directly with the CLR and can also be used to embed Python within .NET applications.","content":"## Introduction\npythonnet is a powerful package that bridges the gap between Python and the .NET Common Language Runtime (CLR). It provides Python programmers with nearly seamless integration with .NET, allowing them to interact with .NET assemblies and objects directly from Python code. Conversely, .NET developers can leverage pythonnet to embed Python as a scripting engine within their .NET applications, opening up new possibilities for extensibility and dynamic behavior.\n\n## Installation\nInstalling pythonnet is straightforward. You can typically install it using pip or conda-forge for Python environments, and via NuGet for .NET projects.\n\nFor Python:\nbash\npip install pythonnet\n\nOr using conda-forge:\nbash\nconda install -c conda-forge pythonnet\n\n\nFor .NET applications, you can add the pythonnet NuGet package. By default, pythonnet uses Mono on Linux and macOS, and .NET Framework on Windows. For specific runtime configurations, such as .NET Core, refer to the official documentation.\n\n## Examples\n\n### Calling .NET Code from Python\npythonnet allows CLR namespaces to be treated essentially as Python packages. You can load assemblies using `clr.AddReference()`.\n\npython\nimport clr\nfrom System import String\nfrom System.Collections import *\n\n# Load a .NET assembly\nclr.AddReference(\"System.Windows.Forms\")\nfrom System.Windows.Forms import Form\n\n# Example for .NET Core runtime\nfrom pythonnet import load\nload(\"coreclr\") # Explicitly load .NET Core\nimport clr\n\n\n### Embedding Python in .NET\nTo embed Python in a .NET application, you need to initialize the Python engine and manage the Global Interpreter Lock (GIL). Remember to set the `Runtime.PythonDLL` property or `PYTHONNET_PYDLL` environment variable before initialization.\n\ncsharp\nusing System;\nusing System.Collections.Generic;\nusing Python.Runtime;\n\npublic class Program\n{\n    public static void Main(string[] args)\n    {\n        // Set Python DLL path, e.g., \"python38.dll\" on Windows\n        // Runtime.PythonDLL = \"python38.dll\"; // Uncomment and set appropriate path\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}\n\n\n## Why Use It\npythonnet offers significant advantages for developers working in both Python and .NET ecosystems. It enables seamless interoperability, allowing you to reuse existing codebases and libraries across languages. Python developers can access the vast array of .NET libraries and frameworks, while .NET developers can integrate Python's powerful scripting capabilities, data science tools, and machine learning libraries directly into their applications. This flexibility fosters greater productivity and expands the possibilities for complex application development.\n\n## Links\nFor more detailed information, documentation, and community support, refer to the following official resources:\n*   [GitHub Repository](https://github.com/pythonnet/pythonnet)\n*   [Wiki](https://github.com/pythonnet/pythonnet/wiki)\n*   [Mailing List](https://mail.python.org/mailman/listinfo/pythondotnet)\n*   [Gitter Chat](https://gitter.im/pythonnet/pythonnet)","metrics":{"detailViews":9,"githubClicks":9},"dates":{"published":null,"modified":"2026-02-09T16:01:12.000Z"}}