Skip to content

Commit 03557b8

Browse files
committed
Improve python selection
- look for micromamba and mamba not just conda - select the current choice when opening the dialog - properly display the version e.g. (3.11.8) Signed-off-by: Geoff Hutchison <[email protected]>
1 parent 45b4c49 commit 03557b8

File tree

1 file changed

+59
-5
lines changed

1 file changed

+59
-5
lines changed

avogadro/qtplugins/configurepython/configurepythondialog.cpp

+59-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88

99
#include "ui_configurepythondialog.h"
1010

11+
#include <avogadro/qtgui/utilities.h>
12+
1113
#include <QDebug>
1214
#include <QFileInfo>
1315
#include <QtCore/QProcess>
1416
#include <QtCore/QSettings>
1517

18+
using Avogadro::QtGui::Utilities::findExecutablePaths;
19+
1620
namespace Avogadro::QtPlugins {
1721

1822
ConfigurePythonDialog::ConfigurePythonDialog(QWidget* aParent)
@@ -33,8 +37,23 @@ ConfigurePythonDialog::ConfigurePythonDialog(QWidget* aParent)
3337
QString condaPath =
3438
settings.value("interpreters/condaPath", "conda").toString();
3539
// check if conda is executable
36-
if (!QFileInfo(condaPath).isExecutable())
37-
return;
40+
if (!QFileInfo(condaPath).isExecutable()) {
41+
// see if we can find any related executables in the path
42+
QStringList names;
43+
names << "micromamba"
44+
<< "mamba"
45+
<< "conda";
46+
#ifdef Q_OS_WIN
47+
names << "micromamba.exe"
48+
<< "mamba.exe"
49+
<< "conda.exe";
50+
#endif
51+
QStringList paths = findExecutablePaths(names);
52+
if (!paths.isEmpty()) {
53+
condaPath = paths.first();
54+
} else
55+
return; // nothing more to do
56+
}
3857

3958
// set the path to conda
4059
settings.setValue("interpreters/condaPath", condaPath);
@@ -85,8 +104,23 @@ void ConfigurePythonDialog::setupCondaEnvironment()
85104
QString condaPath =
86105
settings.value("interpreters/condaPath", "conda").toString();
87106
// check if conda is executable
88-
if (!QFileInfo(condaPath).isExecutable())
89-
return;
107+
if (!QFileInfo(condaPath).isExecutable()) {
108+
// see if we can find any related executables in the path
109+
QStringList names;
110+
names << "micromamba"
111+
<< "mamba"
112+
<< "conda";
113+
#ifdef Q_OS_WIN
114+
names << "micromamba.exe"
115+
<< "mamba.exe"
116+
<< "conda.exe";
117+
#endif
118+
QStringList paths = findExecutablePaths(names);
119+
if (!paths.isEmpty()) {
120+
condaPath = paths.first();
121+
} else
122+
return; // nothing more to do
123+
}
90124

91125
QStringList arguments;
92126
arguments << "create"
@@ -122,9 +156,22 @@ void ConfigurePythonDialog::setOptions(const QStringList& options)
122156
{
123157
m_ui->environmentCombo->clear();
124158

159+
// check the current choice from QSettings
160+
QSettings settings;
161+
QString currentInterpreter =
162+
settings.value("interpreters/python", QString()).toString();
163+
QString currentConda =
164+
settings.value("interpreters/condaEnvironment", QString()).toString();
165+
int index = -1;
166+
125167
// add all conda environments
126168
foreach (const QString& environment, m_condaEnvironments) {
169+
if (environment.isEmpty())
170+
continue; // shouldn't happen, but just in case
171+
127172
m_ui->environmentCombo->addItem(QString("%1 (conda)").arg(environment));
173+
if (environment == currentConda)
174+
index = m_ui->environmentCombo->count() - 1;
128175
}
129176

130177
// get the Python version from each interpreter
@@ -136,7 +183,7 @@ void ConfigurePythonDialog::setOptions(const QStringList& options)
136183
if (process.waitForFinished()) {
137184
QString output = process.readAllStandardOutput();
138185
if (output.startsWith("Python")) {
139-
versions << output.split(" ").at(1);
186+
versions << output.split(" ").at(1).simplified();
140187
} else {
141188
versions << tr("Unknown");
142189
}
@@ -148,9 +195,16 @@ void ConfigurePythonDialog::setOptions(const QStringList& options)
148195
for (int i = 0; i < options.size(); ++i) {
149196
m_ui->environmentCombo->addItem(
150197
QString("%1 (%2)").arg(options.at(i)).arg(versions.at(i)));
198+
// if the conda environment isn't the current, check the python interpreter
199+
if (options.at(i) == currentInterpreter && index == -1)
200+
index = m_ui->environmentCombo->count() - 1;
151201
}
152202

153203
m_ui->environmentCombo->addItem(tr("Other…"));
204+
// set the current choice
205+
if (index >= 0)
206+
m_ui->environmentCombo->setCurrentIndex(index);
207+
154208
m_ui->browseWidget->hide();
155209
}
156210

0 commit comments

Comments
 (0)