- // Check whether the first python in PATH is the right one.
- static string command = python2("python -tt");
-
- if (command.empty()) {
- // It was not, so check whether we can find it elsewhere in
- // PATH, maybe with some suffix appended.
- vector<string> const path = getEnvPath("PATH");
- vector<string>::const_iterator it = path.begin();
- vector<string>::const_iterator const end = path.end();
- lyxerr << "Looking for python v2.x ...\n";
- for (; it != end; ++it) {
- QString const dir = toqstr(*it);
- string const localdir = dir.toLocal8Bit().constData();
- QDir qdir(dir);
- qdir.setFilter(QDir::Files | QDir::Executable);
- QStringList list = qdir.entryList(QStringList("python*"));
- for (int i = 0; i < list.size() && command.empty(); ++i) {
- string const binary = addName(localdir,
- list.at(i).toLocal8Bit().constData());
- command = python2(binary, true);
- }
+ // This function takes inspiration from PEP 394 and PEP 397
+ // PEP 394 -- The "python" Command on Unix-Like Systems
+ // https://www.python.org/dev/peps/pep-0394/
+ // PEP 397 -- Python launcher for Windows
+ // https://www.python.org/dev/peps/pep-0397/
+
+ // Check whether python3 in PATH is the right one.
+ string command = python23_call("python3");
+ if (!command.empty())
+ return command;
+
+ // python3 does not exists, let us try to find python3.x in PATH
+ // the search is probably broader than required
+ // but we are trying hard to find a valid python binary
+ vector<string> const path = getEnvPath("PATH");
+ lyxerr << "Looking for python 3.x ...\n";
+ for (auto bin: path) {
+ QString const dir = toqstr(bin);
+ string const localdir = dir.toLocal8Bit().constData();
+ QDir qdir(dir);
+ qdir.setFilter(QDir::Files | QDir::Executable);
+ QStringList list = qdir.entryList(QStringList("python3*"));
+ for (auto bin: list) {
+ string const binary = addName(localdir,
+ bin.toLocal8Bit().constData());
+ command = python23_call(binary, true);
+ if (!command.empty())
+ return command;