From 40fdae3193d6ca5ee715b5eb23bef7d0a07307a9 Mon Sep 17 00:00:00 2001 From: Olivier Roussel Date: Fri, 8 Dec 2023 08:16:24 +0100 Subject: [PATCH] [Environment] Use standard python paths (#379) * Migrate to standard python modules installation paths. * Remove deprecated fix to import python lib on linux --- Plugin/src/SofaPython3/PythonEnvironment.cpp | 34 +++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/Plugin/src/SofaPython3/PythonEnvironment.cpp b/Plugin/src/SofaPython3/PythonEnvironment.cpp index ce731767..04005a7e 100644 --- a/Plugin/src/SofaPython3/PythonEnvironment.cpp +++ b/Plugin/src/SofaPython3/PythonEnvironment.cpp @@ -180,16 +180,6 @@ void PythonEnvironment::Init() SceneLoaderFactory::getInstance()->addEntry(new SceneLoaderPY3()); } -#if defined(__linux__) - // WARNING: workaround to be able to import python libraries on linux (like - // numpy), at least on Ubuntu (see http://bugs.python.org/issue4434). It is - // not fixing the real problem, but at least it is working for now. - // dmarchal: The problem still exists python3 10/10/2018. - std::string pythonLibraryName = "libpython" + std::string(pythonVersion,0,3) + "m.so"; - dlopen( pythonLibraryName.c_str(), RTLD_LAZY|RTLD_GLOBAL ); - msg_info("SofaPython3") << "Shared library name is '" << pythonLibraryName << "'" ; -#endif - /// Prevent the python terminal from being buffered, not to miss or mix up traces. if( putenv( (char*)"PYTHONUNBUFFERED=1" ) ) msg_warning("SofaPython3") << "failed to set environment variable PYTHONUNBUFFERED"; @@ -386,10 +376,19 @@ void PythonEnvironment::addPythonModulePathsFromDirectory(const std::string& dir return; } + // Using python. directory suffix for modules paths is now the recommanded + // standard location: https://docs.python.org/3.11/install/#how-installation-works and + // https://docs.python.org/3.11/library/site.html#module-site + const auto pythonVersionFull = std::string{Py_GetVersion()}; + const auto pythonVersion = pythonVersionFull.substr(0, pythonVersionFull.find(" ")); // contains major.minor.patch + const auto pythonVersionMajorMinor = pythonVersion.substr(0, pythonVersion.rfind(".")); // contains only manjor.minor + const auto pythonMajorMinorSuffix = "/python" + pythonVersionMajorMinor; + std::vector searchDirs = { directory, directory + "/lib", - directory + "/python3" + directory + pythonMajorMinorSuffix, + directory + "/python3" // deprecated }; // Iterate in the pluginsDirectory and add each sub directory with a 'python' name @@ -401,7 +400,18 @@ void PythonEnvironment::addPythonModulePathsFromDirectory(const std::string& dir const std::string subdir = directory + "/" + *i; if (FileSystem::exists(subdir) && FileSystem::isDirectory(subdir)) { - searchDirs.push_back(subdir + "/python3"); + const std::vector suffixes = { + pythonMajorMinorSuffix, + "/python3" // deprecated + }; + for (const auto& suffix : suffixes) + { + const auto pythonSubdir = subdir + suffix; + if (FileSystem::exists(pythonSubdir) && FileSystem::isDirectory(pythonSubdir)) + { + searchDirs.push_back(pythonSubdir); + } + } } }