jsii Python Client
jsii is an open-source framework developed by AWS that enables code in any language (such as Python, Java, C#, and Go) to naturally interact with JavaScript classes authored in TypeScript. It is a core technology behind the AWS Cloud Development Kit (CDK), allowing polyglot libraries to be delivered from a single TypeScript codebase. The project is actively maintained by AWS, with frequent updates to the compiler, pacmak (bindings generator), and runtime libraries.
Warnings
- breaking The `jsii-pacmak` tool and the Python libraries it generates now require a minimum Python version of 3.8. Previously, this was Python 3.7. It is recommended to upgrade to Python 3.11 or later.
- breaking All `jsii` libraries and tools now require a minimum version of Node.js 18. This was previously Node.js 16. It is recommended to upgrade to Node.js 20 or later, as `jsii` modules rely on an embedded JavaScript VM.
- gotcha When implementing interfaces defined by `jsii` modules in Python, traditional multiple inheritance (e.g., `class MyNewClass(IJsiiInterface):`) will likely cause a metaclass conflict. This is due to `jsii`'s `JSIIMeta` metaclass.
- gotcha When extending or implementing types from `jsii` modules, properties must always be implemented using Python's `@property` decorator to ensure the `jsii` runtime correctly processes access to them by the `jsii` kernel.
- gotcha Passing Python `dict` literals in places where a `jsii` struct is expected might not always work consistently. This can lead to type-checking errors in the `jsii/kernel` module, incorrect translation of property keys (e.g., snake_case to camelCase), or properties being dropped entirely.
- gotcha jsii modules, due to their reliance on an embedded JavaScript engine and marshalling costs, are best suited for development and build tools (like AWS CDK) rather than performance-sensitive or resource-constrained applications.
Install
-
pip install jsii
Imports
- jsii
import jsii
- implements
from jsii import implements
Quickstart
import os
from some_jsii_generated_lib import HelloJsii
# NOTE: In a real scenario, 'some_jsii_generated_lib' would be a library
# generated by jsii (e.g., AWS CDK, or your own jsii module) which has
# 'jsii' as a dependency. The 'HelloJsii' class would be defined in TypeScript.
# Example of using a class from a jsii-generated Python library
try:
# Assuming HelloJsii class and say_hello method are available
# This code is illustrative; replace with actual generated lib usage.
hello_instance = HelloJsii()
message = hello_instance.say_hello("World")
print(message)
except ImportError:
print("To run this, you need a jsii-generated library installed.")
print("For example, 'pip install aws-cdk.core' and then use CDK classes.")
except Exception as e:
print(f"An error occurred: {e}")