Fix Python detection on windows

Patch by Eugene Chornyi
This commit is contained in:
Juergen Spitzmueller 2020-02-15 09:26:08 +01:00
parent 6dab8739eb
commit 195393f401
2 changed files with 35 additions and 8 deletions

View File

@ -635,7 +635,7 @@ string const addName(string const & path, string const & fname)
if (path != "." && path != "./" && !path.empty()) { if (path != "." && path != "./" && !path.empty()) {
buf = os::internal_path(path); buf = os::internal_path(path);
if (!suffixIs(path, '/')) if (!suffixIs(buf, '/'))
buf += '/'; buf += '/';
} }
@ -1039,7 +1039,7 @@ cmd_ret const runCommand(string const & cmd)
command = rtrim(command, "2>&1"); command = rtrim(command, "2>&1");
err2out = true; err2out = true;
} }
string const cmdarg = "/d /c " + command; string const cmdarg = "/d /c \"" + command+"\"";
string const comspec = getEnv("COMSPEC"); string const comspec = getEnv("COMSPEC");
security.nLength = sizeof(SECURITY_ATTRIBUTES); security.nLength = sizeof(SECURITY_ATTRIBUTES);

View File

@ -48,7 +48,7 @@ int timeout_min()
static string const python23_call(string const & binary, bool verbose = false) static string const python23_call(string const & binary, bool verbose = false)
{ {
const string version_info = " -c 'from __future__ import print_function;import sys; print(sys.version_info[:2], end=\"\")'"; const string version_info = " -c \"from __future__ import print_function;import sys; print(sys.version_info[:2], end=\\\"\\\")\"";
// Default to "python" if no binary is given. // Default to "python" if no binary is given.
if (binary.empty()) if (binary.empty())
return "python -tt"; return "python -tt";
@ -89,11 +89,18 @@ static string const find_python_binary()
// PEP 397 -- Python launcher for Windows // PEP 397 -- Python launcher for Windows
// https://www.python.org/dev/peps/pep-0397/ // https://www.python.org/dev/peps/pep-0397/
#ifdef _WIN32
// Check through python launcher whether python 3 is
// installed on computer.
string command = python23_call("py -3");
#else
// Check whether python3 in PATH is the right one. // Check whether python3 in PATH is the right one.
string command = python23_call("python3"); string command = python23_call("python3");
#endif // _WIN32
if (!command.empty()) if (!command.empty())
return command; return command;
#ifndef _WIN32
// python3 does not exists, let us try to find python3.x in PATH // python3 does not exists, let us try to find python3.x in PATH
// the search is probably broader than required // the search is probably broader than required
// but we are trying hard to find a valid python binary // but we are trying hard to find a valid python binary
@ -106,32 +113,52 @@ static string const find_python_binary()
qdir.setFilter(QDir::Files | QDir::Executable); qdir.setFilter(QDir::Files | QDir::Executable);
QStringList list = qdir.entryList(QStringList("python3*")); QStringList list = qdir.entryList(QStringList("python3*"));
for (auto bin2 : list) { for (auto bin2 : list) {
string const binary = addName(localdir, string const binary = "\"" + addName(localdir,
bin2.toLocal8Bit().constData()); bin2.toLocal8Bit().constData()) + "\"";
command = python23_call(binary, true); command = python23_call(binary, true);
if (!command.empty()) if (!command.empty())
return command; return command;
} }
} }
#endif // !_WIN32
// python 3 was not found let us look for python 2 // python 3 was not found let us look for python 2
#ifdef _WIN32
command = python23_call("py -2");
#else
command = python23_call("python2"); command = python23_call("python2");
#endif // _WIN32
if (!command.empty()) if (!command.empty())
return command; return command;
#ifdef _WIN32
// python launcher is not installed, let cmd auto check
// PATH for a python.exe
command = python23_call("python");
if (!command.empty())
return command;
//failed, prepare to search PATH manually
vector<string> const path = getEnvPath("PATH");
lyxerr << "Manually looking for python in PATH ...\n";
QString const exeName = "python*";
#else
// python2 does not exists, let us try to find python2.x in PATH // python2 does not exists, let us try to find python2.x in PATH
// the search is probably broader than required // the search is probably broader than required
// but we are trying hard to find a valid python binary // but we are trying hard to find a valid python binary
lyxerr << "Looking for python 2.x ...\n"; lyxerr << "Looking for python 2.x ...\n";
QString const exeName = "python2*";
#endif // _WIN32
for (auto bin : path) { for (auto bin : path) {
QString const dir = toqstr(bin); QString const dir = toqstr(bin);
string const localdir = dir.toLocal8Bit().constData(); string const localdir = dir.toLocal8Bit().constData();
QDir qdir(dir); QDir qdir(dir);
qdir.setFilter(QDir::Files | QDir::Executable); qdir.setFilter(QDir::Files | QDir::Executable);
QStringList list = qdir.entryList(QStringList("python2*")); QStringList list = qdir.entryList(QStringList(exeName));
for (auto bin2 : list) { for (auto bin2 : list) {
string const binary = addName(localdir, string const binary = "\"" + addName(localdir,
bin2.toLocal8Bit().constData()); bin2.toLocal8Bit().constData()) + "\"";
command = python23_call(binary, true); command = python23_call(binary, true);
if (!command.empty()) if (!command.empty())
return command; return command;