Cannot import dynamically #104

Closed
opened 2025-12-30 01:20:50 +01:00 by adam · 5 comments
Owner

Originally created by @philippemerle on GitHub (Mar 4, 2024).

I would like to write something like

filename = read("prop:source")
template = import(filename)
// Do something on template.

But evaluating this produces the following error

–– Pkl Error ––
Mismatched input: `filename`. Expected: SLQuote

2 | template = import(filename)
                      ^^^^^^^^
at dynamic_import (file:///.../dynamic_import.pkl, line 2)

Is a way to dynamically import a pkl file?

Originally created by @philippemerle on GitHub (Mar 4, 2024). I would like to write something like ``` filename = read("prop:source") template = import(filename) // Do something on template. ``` But evaluating this produces the following error ``` –– Pkl Error –– Mismatched input: `filename`. Expected: SLQuote 2 | template = import(filename) ^^^^^^^^ at dynamic_import (file:///.../dynamic_import.pkl, line 2) ``` Is a way to dynamically import a pkl file?
adam closed this issue 2025-12-30 01:20:50 +01:00
Author
Owner

@stackoverflow commented on GitHub (Mar 4, 2024):

Pkl doesn't support dynamic imports, so you can't do that.
The error message can definitely be improved, though. It's not very clear what the error is.

@stackoverflow commented on GitHub (Mar 4, 2024): Pkl doesn't support dynamic imports, so you can't do that. The error message can definitely be improved, though. It's not very clear what the error is.
Author
Owner

@philippemerle commented on GitHub (Mar 4, 2024):

Thank you for the explanation. This was I thought well.

@philippemerle commented on GitHub (Mar 4, 2024): Thank you for the explanation. This was I thought well.
Author
Owner

@bioball commented on GitHub (Mar 4, 2024):

You can emulate "dynamic imports" using globbed imports.

For example, you can do:

allTemplates = import*("**.pkl")

filename = read("prop:source")

template = allTemplates[filename]

Note: globbed imports resolve to a mapping where the keys are the expanded form of the glob. So, the provided filename must match something that the glob pattern would expand to.

@bioball commented on GitHub (Mar 4, 2024): You can emulate "dynamic imports" using [globbed imports](https://pkl-lang.org/main/current/language-reference/index.html#globbed-imports). For example, you can do: ```groovy allTemplates = import*("**.pkl") filename = read("prop:source") template = allTemplates[filename] ``` Note: globbed imports resolve to a mapping where the keys are the expanded form of the glob. So, the provided `filename` must match something that the glob pattern would expand to.
Author
Owner

@philippemerle commented on GitHub (Mar 4, 2024):

Thank for the proposal. I found another way with a shell script:

#! /bin/sh
uri="file://$(cd "$(dirname "$1")" && pwd -P | sed -- 's/ /%20/g')/$(basename "$1")"
pkl eval - <<EOF
import "$uri" as template
// Do something on template.
EOF
@philippemerle commented on GitHub (Mar 4, 2024): Thank for the proposal. I found another way with a shell script: ```sh #! /bin/sh uri="file://$(cd "$(dirname "$1")" && pwd -P | sed -- 's/ /%20/g')/$(basename "$1")" pkl eval - <<EOF import "$uri" as template // Do something on template. EOF ```
Author
Owner

@bioball commented on GitHub (Mar 4, 2024):

Closing this, as we do not have any plans to implement this.

@bioball commented on GitHub (Mar 4, 2024): Closing this, as we do not have any plans to implement this.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pkl#104