PyScript <py-config>: Local Modules, Interpreter, and Runtime Settings
Use PyScript <py-config> to set the interpreter, import local Python modules, and define environment variables before any <py-script> code runs.
08 Sept 2025, 08:09 UTC

PyScript <py-config>: Local Modules, Interpreter, and Runtime Settings
If you need to import Python files that sit next to your HTML page, or set environment variables before Python code runs, the useful takeaway is that <py-config> does more than declare PyPI packages. It runs during PyScript's bootstrap phase and can select the interpreter binary, add local directories to the import path, define environment variables, and toggle the built-in REPL—all before any <py-script> block executes.
Mechanism: Bootstrap Configuration Before Execution
The <py-config> tag is processed by PyScript while Pyodide initializes. Settings written in TOML or JSON inside the tag are applied to the interpreter before the browser parses <py-script> content. This means local modules declared through paths are importable immediately, and variables in [env] exist when Python code first runs.
Worked Configuration: Local Module and Environment Variable
Assume this project layout:
project/
index.html
lib/
helper.py
lib/helper.py:
def greet(name: str) -> str:
return f"Hello, {name} from a local module"
index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PyScript Local Config</title>
<link rel="stylesheet" href="https://pyscript.net/releases/2024.1.1/pyscript.css" />
<script defer src="https://pyscript.net/releases/2024.1.1/pyscript.js"></script>
<py-config type="toml">
python = "python3"
paths = ["./lib"]
[env]
APP_ENV = "local"
repl = true
</py-config>
</head>
<body>
<py-script>
import os
from helper import greet
print(greet("developer"))
print("APP_ENV:", os.environ.get("APP_ENV"))
</py-script>
</body>
</html>
Here paths = ["./lib"] puts lib/helper.py on Python's import path, so from helper import greet works without a build step. [env] defines APP_ENV as a string that os.environ can read. repl = true adds a browser-based Python REPL to the page, useful for debugging but typically disabled in production.
Limits and Common Mistakes
- Local file access requires an HTTP server: Opening
index.htmldirectly with afile://URL often blocks local module loading because browsers restrict cross-origin requests for local files. Serve the folder withpython3 -m http.server 8000and openhttp://localhost:8000. - Paths are relative to the HTML document:
./libresolves from the page URL, not from the server's working directory. - Environment variables are strings only: Do not expect booleans or integers; cast values in Python if needed.
- Interpreter name must match the PyScript build:
python = "python3"is common, but some releases usepython. An invalid name prevents initialization. - Browser caching may hide edits: Changes to local
.pyfiles can be cached. Use a hard refresh (Ctrl+Shift+R or Cmd+Shift+R) after editing. - REPL exposes a console: Do not rely on
repl = truefor security boundaries; it is a developer convenience.
Verification Steps
- Create the folder structure and files shown above.
- Run
python3 -m http.server 8000in the project directory. No special permissions are required beyond read access to the files. - Open
http://localhost:8000/index.htmlin a modern browser. - Open Developer Tools and select the Console tab.
- Expected check: the console prints the greeting and
APP_ENV: local. If a local module fails, look for aModuleNotFoundError; if the config is invalid, PyScript reports a bootstrap error before Python code runs.
<py-config> Paths vs. Dynamic Imports
Use paths in <py-config> when the helper modules are known before the page loads and you want them available globally. Use imperative imports inside <py-script> only when the module choice depends on runtime input. Unlike micropip package installs, local paths do not download wheels, so they add no network latency beyond fetching the .py files themselves.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.