You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thanks for the awesome project!
I'm trying to embed a python interpreter which is relative to the executable. It works, but I'd like to know if I'm doing this correctly as I feel like there should be a pybind function to target embedded interpreters relative to the executable.
void main() {
// local directory path
fs::path binaryFile = GetExecutablePath();
fs::path binaryDir = binaryFile.parent_path();
// paths which should be in pythons sys path
std::vector<std::string> v = {
(binaryDir / "scripts").string(),
(binaryDir / "python" / "python3.10").string(),
(binaryDir / "python" / "python3.10/site-packages").string(),
(binaryDir / "python" / "python310.zip").string(),
(binaryDir / "python" / "python3.10/lib-dynload").string(),
};
// https://docs.python.org/3/c-api/init.html#c.Py_SetPath
#ifdef _WIN32
std::string str = absl::StrJoin(v, ";");
#else // Assume Linux or macOS
std::string str = absl::StrJoin(v, ":");
#endif
// convert -> widestr -> cstr
std::wstring widestr = std::wstring(str.begin(), str.end());
const wchar_t *widecstr = widestr.c_str();
// Set python path before initialization
Py_SetPath(widecstr);
// Start the interpreter
py::scoped_interpreter guard{};
return 0;
}
Now there are a couple of points:
the sys.executable always points to the executable file, never to the python.exe - is this intended?
is it possible to set the sys.paths via the scoped_interpreter or is there any dedicated function?
The api advices to gather more info in the python doc. With the scoped_interpreter we can use PySys_SetArgvEx to add args to an executed script, but I didn't find an option to set the sys.paths via pybind. Probably Py_SetPath should not be used as the api is only kept for backward compatibility and everything should be done while initialising.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Thanks for the awesome project!
I'm trying to embed a python interpreter which is relative to the executable. It works, but I'd like to know if I'm doing this correctly as I feel like there should be a pybind function to target embedded interpreters relative to the executable.
Now there are a couple of points:
sys.executable
always points to the executable file, never to the python.exe - is this intended?sys.paths
via thescoped_interpreter
or is there any dedicated function?The api advices to gather more info in the python doc. With the
scoped_interpreter
we can use PySys_SetArgvEx to add args to an executed script, but I didn't find an option to set thesys.paths
via pybind. Probably Py_SetPath should not be used as the api is only kept for backward compatibility and everything should be done while initialising.Beta Was this translation helpful? Give feedback.
All reactions