Use more modern way to set Python paths and use PYTHONPATH environment variable when no paths is specified in runner. (#125)

This commit is contained in:
Mikaël Capelle
2024-06-01 13:20:56 +02:00
committed by GitHub
parent 86505f19ce
commit a966db7d19
+25 -11
View File
@@ -82,17 +82,6 @@ namespace mo2::python {
try {
static const char* argv0 = "ModOrganizer.exe";
// initialize the core Path of Python, this must be done before
// initialization
//
if (!pythonPaths.empty()) {
QStringList paths;
for (auto const& p : pythonPaths) {
paths.append(QString::fromStdWString(absolute(p).native()));
}
Py_SetPath(paths.join(';').toStdWString().c_str());
}
PyConfig config;
PyConfig_InitIsolatedConfig(&config);
@@ -104,6 +93,31 @@ namespace mo2::python {
config.site_import = 1;
config.optimization_level = 2;
// set the module search paths
//
auto paths = pythonPaths;
if (paths.empty()) {
// while it is possible to use config.pythonpath_env, it requires
// config.use_environment, which brings other stuffs in and might not be
// what we want, so simply parsing the path ourselve
//
if (auto* pythonPath = std::getenv("PYTHONPATH")) {
for (auto& path : QString::fromStdString(pythonPath).split(";")) {
paths.push_back(
std::filesystem::path{path.trimmed().toStdWString()});
}
}
}
if (!paths.empty()) {
config.module_search_paths_set = 1;
for (auto const& path : paths) {
PyWideStringList_Append(&config.module_search_paths,
absolute(path).native().c_str());
}
}
py::initialize_interpreter(&config, 1, &argv0, true);
if (!Py_IsInitialized()) {