From 90ceb829f347372975310d40d2bb6bade0352890 Mon Sep 17 00:00:00 2001 From: David Shah Date: Wed, 17 Apr 2019 10:57:19 +0100 Subject: pybindings: Fix use of import in user scripts Signed-off-by: David Shah --- common/pybindings.cc | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'common') diff --git a/common/pybindings.cc b/common/pybindings.cc index e1fc8534..f8f366cf 100644 --- a/common/pybindings.cc +++ b/common/pybindings.cc @@ -25,6 +25,7 @@ #include "jsonparse.h" #include "nextpnr.h" +#include #include #include #include @@ -233,6 +234,12 @@ void init_python(const char *executable, bool first) PyImport_AppendInittab(TOSTRING(MODULE_NAME), PYINIT_MODULE_NAME); Py_SetProgramName(program); Py_Initialize(); + + // Add cwd to Python's search path so `import` can be used in user scripts + boost::filesystem::path cwd = boost::filesystem::absolute("./").normalize(); + PyObject *sys_path = PySys_GetObject("path"); + PyList_Insert(sys_path, 0, PyUnicode_FromString(cwd.string().c_str())); + PyImport_ImportModule(TOSTRING(MODULE_NAME)); PyRun_SimpleString("from " TOSTRING(MODULE_NAME) " import *"); } catch (boost::python::error_already_set const &) { -- cgit v1.2.3