From fd4c112436b2478ba58deb787979f6d040f1c836 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20W=C5=82odarczyk?= Date: Mon, 6 Jul 2026 20:07:08 +0200 Subject: [PATCH] scripting: make CWD-local packages importable in embedded Python Embedded CPython builds sys.path from PYTHONHOME only. Unlike the python CLI it does not prepend the working directory, and unlike Windows' getpathp.c it does not add the executable's directory either, so on POSIX "from scripts import ..." (the asset-side scripts package used by texture and screen renderers) fails to import. Insert "." at the front of sys.path after interpreter init, matching how the engine resolves every other asset (relative to CWD). Idempotent and harmless on Windows. --- scripting/PyInt.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/scripting/PyInt.cpp b/scripting/PyInt.cpp index 1474a584..fbbd0b80 100644 --- a/scripting/PyInt.cpp +++ b/scripting/PyInt.cpp @@ -278,6 +278,23 @@ auto python_taskqueue::init() -> bool PyConfig_Clear(&config); } + // make CWD-local packages (e.g. the asset-side "scripts" package used by + // texture/screen renderers) importable. Embedded CPython builds sys.path + // from PYTHONHOME only; unlike the python CLI it does not prepend the + // working directory, and unlike Windows' getpathp.c it does not add the + // executable's directory either, so on POSIX "from scripts import ..." + // would otherwise fail. "." matches how the engine resolves all other + // assets (relative to CWD). Idempotent/harmless on Windows. + if (PyObject *syspath = PySys_GetObject("path")) // borrowed ref + { + PyObject *cwd = PyUnicode_FromString("."); + if (cwd != nullptr) + { + PyList_Insert(syspath, 0, cwd); + Py_DECREF(cwd); + } + } + PyObject *stringiomodule{nullptr}; PyObject *stringioclassname{nullptr}; PyObject *stringioobject{nullptr};