Skip to content

Commit

Permalink
[Environment] Use standard python paths (#379)
Browse files Browse the repository at this point in the history
* Migrate to standard python modules installation paths.

* Remove deprecated fix to import python lib on linux
  • Loading branch information
olivier-roussel authored Dec 8, 2023
1 parent a9f186b commit a282456
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions Plugin/src/SofaPython3/PythonEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -386,10 +376,19 @@ void PythonEnvironment::addPythonModulePathsFromDirectory(const std::string& dir
return;
}

// Using python<MAJOR>.<MINOR> 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<std::string> searchDirs = {
directory,
directory + "/lib",
directory + "/python3"
directory + pythonMajorMinorSuffix,
directory + "/python3" // deprecated
};

// Iterate in the pluginsDirectory and add each sub directory with a 'python' name
Expand All @@ -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<std::string> 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);
}
}
}
}

Expand Down

0 comments on commit a282456

Please sign in to comment.