8 #include "knowrob/integration/python/utils.h"
9 #include "knowrob/URI.h"
10 #include "knowrob/Logger.h"
15 if (modulePath.find(
'/') != std::string::npos) {
20 std::replace(modulePath_withSlash.begin(), modulePath_withSlash.end(),
'.',
'/');
26 static std::set<std::filesystem::path> moduleDirectories;
28 auto topmostPythonPath = modulePath.parent_path();
29 std::list<std::string> modulePathParts;
30 modulePathParts.push_front(modulePath.stem().string());
31 while(!topmostPythonPath.empty() && std::filesystem::exists(topmostPythonPath /
"__init__.py")) {
32 modulePathParts.push_front(topmostPythonPath.stem().string());
33 topmostPythonPath = topmostPythonPath.parent_path();
36 for (
auto it = modulePathParts.begin(); it != modulePathParts.end(); ++it) {
38 if (it != --modulePathParts.end()) {
42 auto relativeModulePath = ss.str();
45 if (moduleDirectories.count(topmostPythonPath) == 0) {
46 moduleDirectories.insert(topmostPythonPath);
48 auto py_sys = boost::python::import(
"sys");
49 auto py_path = py_sys.attr(
"path");
50 auto sysPathAppend = py_path.attr(
"append");
51 sysPathAppend(topmostPythonPath.string());
52 KB_DEBUG(
"[python] Added '{}' to sys.path.", topmostPythonPath.string().c_str());
55 return relativeModulePath;
static std::string resolve(const std::string_view &uriString)
std::string addToSysPath(const std::filesystem::path &modulePath)
std::string resolveModulePath(std::string_view modulePath)