DotnetPythonHandler 1.0.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package DotnetPythonHandler --version 1.0.4
                    
NuGet\Install-Package DotnetPythonHandler -Version 1.0.4
                    
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.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DotnetPythonHandler" Version="1.0.4" />
                    
Directory.Packages.props
<PackageReference Include="DotnetPythonHandler" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DotnetPythonHandler --version 1.0.4
                    
#r "nuget: DotnetPythonHandler, 1.0.4"
                    
#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.
#addin nuget:?package=DotnetPythonHandler&version=1.0.4
                    
Install as a Cake Addin
#tool nuget:?package=DotnetPythonHandler&version=1.0.4
                    
Install as a Cake Tool

<a name='assembly'></a>

PythonHandler

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

Create an IronPython Handler

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

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

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);

Contents

<a name='T-PythonHandler-Handler'></a>

Handler type

Namespace

PythonHandler

Summary

Class Handler handles execution of Python scripts using IronPython.

<a name='M-PythonHandler-Handler-#ctor'></a>

#ctor() constructor

Summary

This constructor initializes a new Handler.

Parameters

This constructor has no parameters.

<a name='M-PythonHandler-Handler-#ctor-System-Collections-Generic-ICollection{System-String}-'></a>

#ctor(additionalSearchPaths) constructor

Summary

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

Parameters
Name Type Description
additionalSearchPaths System.Collections.Generic.ICollection{System.String} additionalSearchPaths is a collection of addtional module search paths to add to the IronPython scope.

<a name='F-PythonHandler-Handler-_scriptEngine'></a>

_scriptEngine constants

Summary

Instance variable _scriptEngine is the IronPython ScriptEngine used for hosting IronPython.

<a name='M-PythonHandler-Handler-RunFromFile-System-String-'></a>

RunFromFile(scriptFile) method

Summary

This method runs a Python script.

Parameters
Name Type Description
scriptFile System.String scriptFile is the path to the Python script to execute.

<a name='M-PythonHandler-Handler-RunFromFile-System-String,System-Collections-Generic-Dictionary{System-String,System-Object}-'></a>

RunFromFile(scriptFile,variables) method

Summary

This method runs a Python script.

Parameters
Name Type Description
scriptFile System.String scriptFile is the path to the Python script to execute.
variables System.Collections.Generic.Dictionary{System.String,System.Object} variables is an Dictionary containing the key/values of variables to set.

<a name='M-PythonHandler-Handler-RunFromFile``1-System-String,System-String-'></a>

RunFromFile``1(scriptFile,returnVariable) method

Summary

This method runs a Python script.

Returns

The value of the returnVariable from the scope.

Parameters
Name Type Description
scriptFile System.String scriptFile is the path to the Python script to execute.
returnVariable System.String returnVariable is the variable to read from the scope.
Generic Types
Name Description
TResult The type of data to return.
Exceptions
Name Description
System.Collections.Generic.KeyNotFoundException The returnVariable was not found.

<a name='M-PythonHandler-Handler-RunFromFile``1-System-String,System-String,System-Collections-Generic-Dictionary{System-String,System-Object}-'></a>

RunFromFile``1(scriptFile,returnVariable,variables) method

Summary

This method runs a Python script.

Returns

The value of the returnVariable from the scope.

Parameters
Name Type Description
scriptFile System.String scriptFile is the path to the Python script to execute.
returnVariable System.String returnVariable is the variable to read from the scope.
variables System.Collections.Generic.Dictionary{System.String,System.Object} variables is an Dictionary containing the key/values of variables to set.
Generic Types
Name Description
TResult The type of data to return.
Exceptions
Name Description
System.Collections.Generic.KeyNotFoundException The returnVariable was not found.

<a name='M-PythonHandler-Handler-RunFromString-System-String-'></a>

RunFromString(code) method

Summary

This method runs a string of Python code.

Parameters
Name Type Description
code System.String code is the Python string to execute.

<a name='M-PythonHandler-Handler-RunFromString-System-String,System-Collections-Generic-Dictionary{System-String,System-Object}-'></a>

RunFromString(code,variables) method

Summary

This method runs a string of Python code.

Parameters
Name Type Description
code System.String code is the Python string to execute.
variables System.Collections.Generic.Dictionary{System.String,System.Object} variables is an Dictionary containing the key/values of variables to set.

<a name='M-PythonHandler-Handler-RunFromString``1-System-String,System-String-'></a>

RunFromString``1(code,returnVariable) method

Summary

This method runs a string of Python code.

Returns

The value of the returnVariable from the scope.

Parameters
Name Type Description
code System.String code is the Python string to execute.
returnVariable System.String returnVariable is the variable to read from the scope.
Generic Types
Name Description
TResult The type of data to return.
Exceptions
Name Description
System.Collections.Generic.KeyNotFoundException The returnVariable was not found.

<a name='M-PythonHandler-Handler-RunFromString``1-System-String,System-String,System-Collections-Generic-Dictionary{System-String,System-Object}-'></a>

RunFromString``1(code,returnVariable,variables) method

Summary

This method runs a string of Python code.

Returns

The value of the returnVariable from the scope.

Parameters
Name Type Description
code System.String code is the Python string to execute.
returnVariable System.String returnVariable is the variable to read from the scope.
variables System.Collections.Generic.Dictionary{System.String,System.Object} variables is an Dictionary containing the key/values of variables to set.
Generic Types
Name Description
TResult The type of data to return.
Exceptions
Name Description
System.Collections.Generic.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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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 362 1/3/2023
1.0.4 318 12/23/2022
1.0.3 342 12/22/2022
1.0.2 336 12/22/2022
1.0.1 330 12/22/2022
1.0.0 340 12/21/2022