DotnetPythonHandler 1.1.0

dotnet add package DotnetPythonHandler --version 1.1.0
NuGet\Install-Package DotnetPythonHandler -Version 1.1.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="DotnetPythonHandler" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DotnetPythonHandler --version 1.1.0
#r "nuget: DotnetPythonHandler, 1.1.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 DotnetPythonHandler as a Cake Addin
#addin nuget:?package=DotnetPythonHandler&version=1.1.0

// Install DotnetPythonHandler as a Cake Tool
#tool nuget:?package=DotnetPythonHandler&version=1.1.0

PythonHandler

This package is a wrapper around IronPython/CPython allowing for easily running Python scripts.

Validate Release

Create a Handler (IronPython)

using PythonHandler;

// Create a handler without additional module search paths.
Handler pythonHandler = new Handler();

// Create a handler with additional module search paths.
var searchPaths = new List<string>() {"/custom/python/lib"}
Handler pythonHandler = new Handler(searchPaths);

Run from String (IronPython)

using PythonHandler;

Handler pythonHandler = new Handler();

// Run an IronPython script from a string without a return value.
pythonHandler.RunFromString("import sys; sysversion = sys.version");

// Run an IronPython script from a string with a return value.
var result = pythonHandler.RunFromString<string>("import sys; sysversion = sys.version", "sysversion");

// Run an IronPython script from a string with a return value and additional variables.
var tdict = new Dictionary<string, object>();
tdict.Add("testvar", "1ece43cc-b45a-4f97-ba69-5106f23e3932");
var result = pythonHandler.RunFromString<string>("import sys", "testvar", tdict);

Run from File (IronPython)

using PythonHandler;

Handler pythonHandler = new Handler();

// Run an IronPython script from a file without a return value.
pythonHandler.RunFromFile("import sys; sysversion = sys.version");

// Run an IronPython script from a file with a return value.
var result = pythonHandler.RunFromFile<string>("import sys; sysversion = sys.version", "sysversion");

// Run an IronPython script from a file with a return value and additional variables.
var tdict = new Dictionary<string, object>();
tdict.Add("testvar", "1ece43cc-b45a-4f97-ba69-5106f23e3932");
var result = pythonHandler.RunFromFile<string>("import sys", "testvar", tdict);

Create a CHandler (CPython)

using PythonHandler;

// Create a handler without additional module search paths.
CHandler pythonHandler = new CHandler();

// Create a handler with additional module search paths.
var searchPaths = new List<string>() {"/custom/python/lib"}
CHandler pythonHandler = new CHandler(searchPaths);

Run from String (CPython)

using PythonHandler;

CHandler pythonHandler = new CHandler();

// Run a CPython script from a string without a return value.
pythonHandler.RunFromString("import sys; sysversion = sys.version");

// Run a CPython script from a string with a return value.
var result = pythonHandler.RunFromString<string>("import sys; sysversion = sys.version", "sysversion");

// Run a CPython script from a string with a return value and additional variables.
var tdict = new Dictionary<string, object>();
tdict.Add("testvar", "1ece43cc-b45a-4f97-ba69-5106f23e3932");
var result = pythonHandler.RunFromString<string>("import sys", "testvar", tdict);

Run from File (CPython)

using PythonHandler;

CHandler pythonHandler = new CHandler();

// Run a CPython script from a file without a return value.
pythonHandler.RunFromFile("import sys; sysversion = sys.version");

// Run a CPython script from a file with a return value.
var result = pythonHandler.RunFromFile<string>("import sys; sysversion = sys.version", "sysversion");

// Run a CPython script from a file with a return value and additional variables.
var tdict = new Dictionary<string, object>();
tdict.Add("testvar", "1ece43cc-b45a-4f97-ba69-5106f23e3932");
var result = pythonHandler.RunFromFile<string>("import sys", "testvar", tdict);

Contents

Classes

CHandler

Class CHandler handles execution of Python scripts using a local Python setup.

Constructor & Destructor Documentation
PythonHandler.CHandler.CHandler (string pythonDllPath)

This constructor initializes a new CHandler.

Parameters

pythonDllPath The local path of the Python DLL to use for the runtime.

PythonHandler.CHandler.CHandler (string pythonDllPath, string pythonHome)

This constructor initializes a new CHandler.

Parameters

pythonDllPath The local path of the Python DLL to use for the runtime.
pythonHome The path to the python home directory.

Member Function Documentation
void PythonHandler.CHandler.RunFromFile (string scriptFile)

This method runs a Python script.

Parameters

scriptFile scriptFile is the path to the Python script to execute.

void PythonHandler.CHandler.RunFromFile (string scriptFile, Dictionary< string, object > variables)

This method runs a Python script.

Parameters

scriptFile scriptFile is the path to the Python script to execute.
variables variables is an Dictionary containing the key/values of variables to set.

TResult PythonHandler.CHandler.RunFromFile< TResult > (string scriptFile, string returnVariable)

This method runs a Python script.

Parameters

scriptFile scriptFile is the path to the Python script to execute.
returnVariable returnVariable is the variable to read from the scope.

Exceptions

KeyNotFoundException The returnVariable was not found.

Returns

The value of the returnVariable from the scope.

TResult PythonHandler.CHandler.RunFromFile< TResult > (string scriptFile, string returnVariable, Dictionary< string, object > variables)

This method runs a Python script.

Parameters

scriptFile scriptFile is the path to the Python script to execute.
returnVariable returnVariable is the variable to read from the scope.
variables variables is an Dictionary containing the key/values of variables to set.

Returns

The value of the returnVariable from the scope.

Exceptions

KeyNotFoundException The returnVariable was not found.

void PythonHandler.CHandler.RunFromString (string code)

This method runs a string of Python code.

Parameters

code code is the Python string to execute.

void PythonHandler.CHandler.RunFromString (string code, Dictionary< string, object > variables)

This method runs a string of Python code.

Parameters

code code is the Python string to execute.
variables variables is an Dictionary containing the key/values of variables to set.

TResult PythonHandler.CHandler.RunFromString< TResult > (string code, string returnVariable)

This method runs a string of Python code.

Parameters

code code is the Python string to execute.
returnVariable returnVariable is the variable to read from the scope.

Returns

The value of the returnVariable from the scope.

Exceptions

KeyNotFoundException The returnVariable was not found.

TResult PythonHandler.CHandler.RunFromString< TResult > (string code, string returnVariable, Dictionary< string, object > variables)

This method runs a string of Python code.

Parameters

code code is the Python string to execute.
returnVariable returnVariable is the variable to read from the scope.
variables variables is an Dictionary containing the key/values of variables to set.

Exceptions

KeyNotFoundException The returnVariable was not found.

Returns

The value of the returnVariable from the scope.

Handler

Class Handler handles execution of Python scripts using IronPython.

Constructor & Destructor Documentation
PythonHandler.Handler.Handler (ICollection< string > additionalSearchPaths)

This constructor initializes a new Handler with additional module search paths.

Parameters

additionalSearchPaths additionalSearchPaths is a collection of addtional module search paths to add to the IronPython scope.

Member Function Documentation
void PythonHandler.Handler.AppendSearchPath (string searchPath)

This method appends a search path to the engine.

Parameters

searchPath searchPath is the new search path to append to the engine.

void PythonHandler.Handler.RunFromFile (string scriptFile)

This method runs a Python script.

Parameters

scriptFile scriptFile is the path to the Python script to execute.

void PythonHandler.Handler.RunFromFile (string scriptFile, Dictionary< string, object > variables)

This method runs a Python script.

Parameters

scriptFile scriptFile is the path to the Python script to execute.
variables variables is an Dictionary containing the key/values of variables to set.

TResult PythonHandler.Handler.RunFromFile< TResult > (string scriptFile, string returnVariable)

This method runs a Python script.

Template Parameters

TResult The type of data to return.

Parameters

scriptFile scriptFile is the path to the Python script to execute.
returnVariable returnVariable is the variable to read from the scope.

Returns

The value of the returnVariable from the scope.

Exceptions

KeyNotFoundException The returnVariable was not found.

TResult PythonHandler.Handler.RunFromFile< TResult > (string scriptFile, string returnVariable, Dictionary< string, object > variables)

This method runs a Python script.

Template Parameters

TResult The type of data to return.

Parameters

scriptFile scriptFile is the path to the Python script to execute.
returnVariable returnVariable is the variable to read from the scope.
variables variables is an Dictionary containing the key/values of variables to set.

Returns

The value of the returnVariable from the scope.

Exceptions

KeyNotFoundException The returnVariable was not found.

void PythonHandler.Handler.RunFromString (string code)

This method runs a string of Python code.

Parameters

code code is the Python string to execute.

void PythonHandler.Handler.RunFromString (string code, Dictionary< string, object > variables)

This method runs a string of Python code.

Parameters

code code is the Python string to execute.
variables variables is an Dictionary containing the key/values of variables to set.

TResult PythonHandler.Handler.RunFromString< TResult > (string code, string returnVariable)

This method runs a string of Python code.

Template Parameters

TResult The type of data to return.

Parameters

code code is the Python string to execute.
returnVariable returnVariable is the variable to read from the scope.

Returns

The value of the returnVariable from the scope.

Exceptions

KeyNotFoundException The returnVariable was not found.

TResult PythonHandler.Handler.RunFromString< TResult > (string code, string returnVariable, Dictionary< string, object > variables)

This method runs a string of Python code.

Template Parameters

TResult The type of data to return.

Parameters

code code is the Python string to execute.
returnVariable returnVariable is the variable to read from the scope.
variables variables is an Dictionary containing the key/values of variables to set.

Returns

The value of the returnVariable from the scope.

Exceptions

KeyNotFoundException The returnVariable was not found.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  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.1.0 294 1/3/2023
1.0.4 258 12/23/2022
1.0.3 283 12/22/2022
1.0.2 275 12/22/2022
1.0.1 269 12/22/2022
1.0.0 279 12/21/2022