Skip to content

Commit b95645b

Browse files
authored
Merge pull request #1935 from ghutchis/tweak-python-launch
Tweak search for Python paths - make sure to check it exists
2 parents 1d7c84b + 7cdd095 commit b95645b

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

avogadro/qtplugins/configurepython/configurepython.cpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
#include <QAction>
1515
#include <QtCore/QDebug>
16+
#include <QtCore/QFileInfo>
17+
#include <QtCore/QProcess>
1618
#include <QtCore/QSettings>
1719
#include <QtCore/QSysInfo>
1820
#include <QtCore/QUrl>
@@ -148,11 +150,39 @@ QStringList ConfigurePython::pythonPaths() const
148150

149151
QStringList paths = findExecutablePaths(names);
150152

151-
// Add the current interpreter to the list if it's not already there.
152-
if (!paths.contains(pythonInterp))
153+
// Add the current interpreter to the list
154+
// it may be filtered out by the loop below
155+
if (!paths.contains(pythonInterp)) {
153156
paths.prepend(pythonInterp);
157+
}
158+
159+
// check to make sure each of the items are valid or remove them
160+
// (i.e., the python should return a version flag)
161+
QStringList validPaths;
162+
QStringList arguments;
163+
arguments << "-V";
164+
foreach (const QString& path, paths) {
165+
QFileInfo info(path);
166+
if (info.exists() && info.isExecutable()) {
167+
// try to run it to get the version
168+
QProcess process;
169+
process.start(path, arguments);
170+
if (process.waitForFinished()) {
171+
QString output = process.readAllStandardOutput();
172+
// should be like Python 3.10.14
173+
if (output.startsWith("Python")) {
174+
QString version = output.split(" ").at(1).simplified();
175+
// make sure it's at least Python 3
176+
// in the future, we can ensure particular releases
177+
if (version.startsWith("3"))
178+
validPaths << path;
179+
}
180+
}
181+
// if we didn't get results, it's not valid
182+
}
183+
}
154184

155-
return paths;
185+
return validPaths;
156186
}
157187

158188
void ConfigurePython::showDialog()

0 commit comments

Comments
 (0)