Skip to content

Commit a966db7

Browse files
authored
Use more modern way to set Python paths and use PYTHONPATH environment variable when no paths is specified in runner. (#125)
1 parent 86505f1 commit a966db7

1 file changed

Lines changed: 25 additions & 11 deletions

File tree

src/runner/pythonrunner.cpp

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,6 @@ namespace mo2::python {
8282
try {
8383
static const char* argv0 = "ModOrganizer.exe";
8484

85-
// initialize the core Path of Python, this must be done before
86-
// initialization
87-
//
88-
if (!pythonPaths.empty()) {
89-
QStringList paths;
90-
for (auto const& p : pythonPaths) {
91-
paths.append(QString::fromStdWString(absolute(p).native()));
92-
}
93-
Py_SetPath(paths.join(';').toStdWString().c_str());
94-
}
95-
9685
PyConfig config;
9786
PyConfig_InitIsolatedConfig(&config);
9887

@@ -104,6 +93,31 @@ namespace mo2::python {
10493
config.site_import = 1;
10594
config.optimization_level = 2;
10695

96+
// set the module search paths
97+
//
98+
auto paths = pythonPaths;
99+
if (paths.empty()) {
100+
101+
// while it is possible to use config.pythonpath_env, it requires
102+
// config.use_environment, which brings other stuffs in and might not be
103+
// what we want, so simply parsing the path ourselve
104+
//
105+
if (auto* pythonPath = std::getenv("PYTHONPATH")) {
106+
for (auto& path : QString::fromStdString(pythonPath).split(";")) {
107+
paths.push_back(
108+
std::filesystem::path{path.trimmed().toStdWString()});
109+
}
110+
}
111+
}
112+
113+
if (!paths.empty()) {
114+
config.module_search_paths_set = 1;
115+
for (auto const& path : paths) {
116+
PyWideStringList_Append(&config.module_search_paths,
117+
absolute(path).native().c_str());
118+
}
119+
}
120+
107121
py::initialize_interpreter(&config, 1, &argv0, true);
108122

109123
if (!Py_IsInitialized()) {

0 commit comments

Comments
 (0)