PythonNet.Helper 1.0.0

dotnet add package PythonNet.Helper --version 1.0.0
NuGet\Install-Package PythonNet.Helper -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PythonNet.Helper" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PythonNet.Helper --version 1.0.0
#r "nuget: PythonNet.Helper, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PythonNet.Helper as a Cake Addin
#addin nuget:?package=PythonNet.Helper&version=1.0.0

// Install PythonNet.Helper as a Cake Tool
#tool nuget:?package=PythonNet.Helper&version=1.0.0

PythonNet.Helper

Bring Python to C# !!!

Nuget

This library is a help library for PythonNet, if you find any bugs, please report them. Also let me know if you have any comments

Install via NuGet

To install Atrob, run the following command in the Package Manager Console :

PM> Install-Package PythonNet.Helper

Usage

To use this library, you must have .NET and Python installed

To start, make an instance of PyNet as below and give the Python DLL path

IPyNet pyNet = new PyNet(@"C:\Program Files\Python311\python311.dll");

Now you can use RunPython which has many overloads whose parameter list and input type are listed below

Name Type Description
pythonCode string Python code to be executed
parameters IDictionary<string, object> The list of input parameters in the form of a dictionary, which is the key of the variable name in Python
returnedVariableNames string[] The names of the variables that are supposed to be taken from the Python code
returnedVariableNames string[] The names of the variables that are supposed to be taken from the Python code
parameter (string key, object value) To pass a parameter instead of using a dictionary
returnedVariableName string To get just one return value instead of using an array

Samples

Run

pyNet.RunPython("print('Welcome To PythonNet.Helper')");

Run From File

You can give the address of the code file and use ReadFile to read the code as below

pyNet.RunPython("PythonFiles/HelloWorld.py".ReadFile());

By Parameters

  • Parameter
pyNet.RunPython(@$"print('Hello ' + name)", ("name", "Mohammad Taqi"));
  • Parameters
 pyNet.RunPython("print(fname + ' ' + lname)", new Dictionary<string, object>
{
    ["fname"] = "Mohammad Taqi",
    ["lname"] = "Nasiri"
});
  • Model parameter
public class UserModel
{
    public string Name { get; set; } = string.Empty;
    public string Family { get; set; } = string.Empty;
    public DateTime BirthDay { get; set; }
}
pyNet.RunPython("print(f'Name : {user.Name} | Family : {user.Family} | BirthDay : {user.BirthDay}')",("user",new UserModel()
{
    Name = "Mohammad Taqi",
    Family = "Nasiri",
    BirthDay = DateTime.Now
}));

By returned Variables

  • Without parameter
pyNet.RunPython("from datetime import date\n" + "today = date.today()", "today")
  • By parameter
int sumNumbers = Convert.ToInt32(pyNet.RunPython("sum = sum(nums)", ("nums", new List<int>() { 1, 5, 8, 7, 15, 20, 36 }), "sum").ToString())

You can also pass multiple parameters with a dictionary

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 218 3/25/2023