pythonnet 3.1.0-preview2023-10-03
pythonnet is a package that gives .NET programmers ability to
integrate Python engine and use Python libraries.
Embedding Python in .NET
- You must set Runtime.PythonDLLproperty orPYTHONNET_PYDLLenvironment variable, otherwise you will receiveBadPythonDllException(internal, derived fromMissingMethodException) upon callingInitialize. Typical values arepython38.dll(Windows),libpython3.8.dylib(Mac),libpython3.8.so(most other *nix). Full path may be required.
- Then call PythonEngine.Initialize(). If you plan to use Python objects from multiple threads, also callPythonEngine.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, egmod.func(args). You can also access Python objects viaPyObjectand dervied types instead of usingdynamic.
- Use mod.func(args, Py.kw("keywordargname", keywordargvalue))ormod.func(args, keywordargname: keywordargvalue)to apply keyword arguments.
- Mathematical operations involving python and literal/managed types
must have the python object first, eg. np.pi * 2works,2 * np.pidoesn't.
Example
using var _ = 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:
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.
No packages depend on pythonnet.
.NET Standard 2.0
- Microsoft.CSharp (>= 4.7.0)
- System.Reflection.Emit (>= 4.3.0)
| Version | Downloads | Last updated | 
|---|---|---|
| 3.1.0-preview2024-09-06 | 8 | 06/28/2025 | 
| 3.1.0-preview2024-08-05 | 9 | 06/28/2025 | 
| 3.1.0-preview2024-07-03 | 10 | 06/28/2025 | 
| 3.1.0-preview2024-06-03 | 9 | 06/28/2025 | 
| 3.1.0-preview2024-05-07 | 8 | 06/28/2025 | 
| 3.1.0-preview2024-04-07 | 9 | 06/28/2025 | 
| 3.1.0-preview2024-03-03 | 9 | 06/28/2025 | 
| 3.1.0-preview2024-02-05 | 10 | 06/28/2025 | 
| 3.1.0-preview2024-01-04 | 9 | 06/28/2025 | 
| 3.1.0-preview2023-12-04 | 9 | 06/28/2025 | 
| 3.1.0-preview2023-11-04 | 9 | 06/28/2025 | 
| 3.1.0-preview2023-10-03 | 10 | 06/28/2025 | 
| 3.1.0-preview2023-09-17 | 8 | 06/28/2025 | 
| 3.1.0-preview2023-07-03 | 9 | 06/28/2025 | 
| 3.1.0-preview2023-04-03 | 9 | 06/28/2025 | 
| 3.0.5 | 10 | 06/13/2025 | 
| 3.0.4 | 10 | 06/28/2025 | 
| 3.0.3 | 10 | 06/28/2025 | 
| 3.0.2 | 9 | 06/28/2025 | 
| 3.0.1 | 9 | 06/28/2025 | 
| 3.0.0 | 11 | 06/28/2025 | 
| 2.3.0-py35-dotnet | 7 | 06/28/2025 | 
| 2.3.0-py27-dotnet | 9 | 06/28/2025 |