Power Fx Python Bridge
The `powerfx` Python library (current version 0.0.34) acts as a bridge to invoke the C# implementation of Microsoft Power Fx, a low-code, general-purpose programming language. It enables Python applications to evaluate Power Fx expressions. This library is actively maintained by Microsoft and sees releases periodically, often tied to updates within the broader Power Platform ecosystem.
Common errors
-
RuntimeError: Can not determine dotnet root
cause The `pythonnet` library, used by `powerfx` to bridge to the C# implementation, cannot locate the .NET runtime installation on your system.fixInstall the .NET SDK or runtime for your operating system and architecture. If it's not installed in a default path, set the `DOTNET_ROOT` environment variable to the path where .NET is installed (e.g., `export DOTNET_ROOT=/usr/local/share/dotnet`). -
FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=..., Culture=neutral, PublicKeyToken=...' or one of its dependencies.
cause This typically indicates an incompatibility between the installed .NET runtime and the version of the `powerfx` library's C# components, or a corrupted .NET installation.fixVerify that your .NET runtime version is compatible with the `powerfx` library. Try updating your .NET runtime to the latest stable version or reinstalling it. Also, consider reinstalling `powerfx` and its dependencies (`pip uninstall powerfx pythonnet` then `pip install powerfx`).
Warnings
- breaking The `powerfx` library is a Python bridge to a C# implementation. Major updates to the underlying Power Fx language or the C# runtime it depends on may introduce breaking changes not directly reflected in the Python package versioning.
- gotcha Requires .NET Runtime: The `powerfx` Python package will not function without a compatible .NET runtime installed and correctly configured on the system. This is a common point of failure for users.
- gotcha Power Fx is a declarative, Excel-like language. Trying to use it with imperative Python programming paradigms or expecting direct object manipulation as in Python will lead to unexpected results. It's not a Pythonic library in its core logic.
Install
-
pip install powerfx
Imports
- Engine
from powerfx import Engine
Quickstart
import os
from powerfx import Engine
# Ensure DOTNET_ROOT is set if .NET runtime is not in standard paths
# Example: os.environ['DOTNET_ROOT'] = '/usr/local/share/dotnet'
# Initialize the Power Fx engine
engine = Engine()
# Define variables that can be used in Power Fx expressions
engine.add_host_object('Name', 'Alice')
engine.add_host_object('Age', 30)
# Evaluate a simple Power Fx expression
expression = '"Hello " & Name & ", you are " & Text(Age) & " years old!"'
result = engine.eval(expression)
print(f"Power Fx Expression: {expression}")
print(f"Result: {result.to_string()}")
# Example with a conditional expression
conditional_expression = 'If(Age >= 18, "Adult", "Minor")'
conditional_result = engine.eval(conditional_expression)
print(f"\nPower Fx Conditional Expression: {conditional_expression}")
print(f"Conditional Result: {conditional_result.to_string()}")