Skip to content

Commit a282456

Browse files
[Environment] Use standard python paths (#379)
* Migrate to standard python modules installation paths. * Remove deprecated fix to import python lib on linux
1 parent a9f186b commit a282456

1 file changed

Lines changed: 22 additions & 12 deletions

File tree

Plugin/src/SofaPython3/PythonEnvironment.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,6 @@ void PythonEnvironment::Init()
180180
SceneLoaderFactory::getInstance()->addEntry(new SceneLoaderPY3());
181181
}
182182

183-
#if defined(__linux__)
184-
// WARNING: workaround to be able to import python libraries on linux (like
185-
// numpy), at least on Ubuntu (see http://bugs.python.org/issue4434). It is
186-
// not fixing the real problem, but at least it is working for now.
187-
// dmarchal: The problem still exists python3 10/10/2018.
188-
std::string pythonLibraryName = "libpython" + std::string(pythonVersion,0,3) + "m.so";
189-
dlopen( pythonLibraryName.c_str(), RTLD_LAZY|RTLD_GLOBAL );
190-
msg_info("SofaPython3") << "Shared library name is '" << pythonLibraryName << "'" ;
191-
#endif
192-
193183
/// Prevent the python terminal from being buffered, not to miss or mix up traces.
194184
if( putenv( (char*)"PYTHONUNBUFFERED=1" ) )
195185
msg_warning("SofaPython3") << "failed to set environment variable PYTHONUNBUFFERED";
@@ -386,10 +376,19 @@ void PythonEnvironment::addPythonModulePathsFromDirectory(const std::string& dir
386376
return;
387377
}
388378

379+
// Using python<MAJOR>.<MINOR> directory suffix for modules paths is now the recommanded
380+
// standard location: https://docs.python.org/3.11/install/#how-installation-works and
381+
// https://docs.python.org/3.11/library/site.html#module-site
382+
const auto pythonVersionFull = std::string{Py_GetVersion()};
383+
const auto pythonVersion = pythonVersionFull.substr(0, pythonVersionFull.find(" ")); // contains major.minor.patch
384+
const auto pythonVersionMajorMinor = pythonVersion.substr(0, pythonVersion.rfind(".")); // contains only manjor.minor
385+
const auto pythonMajorMinorSuffix = "/python" + pythonVersionMajorMinor;
386+
389387
std::vector<std::string> searchDirs = {
390388
directory,
391389
directory + "/lib",
392-
directory + "/python3"
390+
directory + pythonMajorMinorSuffix,
391+
directory + "/python3" // deprecated
393392
};
394393

395394
// Iterate in the pluginsDirectory and add each sub directory with a 'python' name
@@ -401,7 +400,18 @@ void PythonEnvironment::addPythonModulePathsFromDirectory(const std::string& dir
401400
const std::string subdir = directory + "/" + *i;
402401
if (FileSystem::exists(subdir) && FileSystem::isDirectory(subdir))
403402
{
404-
searchDirs.push_back(subdir + "/python3");
403+
const std::vector<std::string> suffixes = {
404+
pythonMajorMinorSuffix,
405+
"/python3" // deprecated
406+
};
407+
for (const auto& suffix : suffixes)
408+
{
409+
const auto pythonSubdir = subdir + suffix;
410+
if (FileSystem::exists(pythonSubdir) && FileSystem::isDirectory(pythonSubdir))
411+
{
412+
searchDirs.push_back(pythonSubdir);
413+
}
414+
}
405415
}
406416
}
407417

0 commit comments

Comments
 (0)