Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Environment] Use standard python paths #379

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading