Allow users to specify Python executable path

Closes #19195.
PR #19644.
This commit is contained in:
Chocobo1
2023-09-28 01:27:48 +08:00
committed by GitHub
parent 529e49aea7
commit b3fda76027
10 changed files with 77 additions and 16 deletions

View File

@@ -44,6 +44,8 @@
#include "base/global.h"
#include "base/logger.h"
#include "base/path.h"
#include "base/preferences.h"
#include "base/utils/bytearray.h"
using namespace Utils::ForeignApps;
@@ -52,6 +54,8 @@ namespace
{
bool testPythonInstallation(const QString &exeName, PythonInfo &info)
{
info = {};
QProcess proc;
proc.start(exeName, {u"--version"_s}, QIODevice::ReadOnly);
if (proc.waitForFinished() && (proc.exitCode() == QProcess::NormalExit))
@@ -77,7 +81,7 @@ namespace
return false;
info = {exeName, version};
LogMsg(QCoreApplication::translate("Utils::ForeignApps", "Python detected, executable name: '%1', version: %2")
LogMsg(QCoreApplication::translate("Utils::ForeignApps", "Found Python executable. Name: \"%1\". Version: \"%2\"")
.arg(info.executableName, info.version.toString()), Log::INFO);
return true;
}
@@ -254,20 +258,43 @@ bool Utils::ForeignApps::PythonInfo::isSupportedVersion() const
PythonInfo Utils::ForeignApps::pythonInfo()
{
static PythonInfo pyInfo;
if (!pyInfo.isValid())
{
if (testPythonInstallation(u"python3"_s, pyInfo))
return pyInfo;
if (testPythonInstallation(u"python"_s, pyInfo))
const QString preferredPythonPath = Preferences::instance()->getPythonExecutablePath().toString();
if (pyInfo.isValid() && (preferredPythonPath == pyInfo.executableName))
return pyInfo;
if (!preferredPythonPath.isEmpty())
{
if (testPythonInstallation(preferredPythonPath, pyInfo))
return pyInfo;
LogMsg(QCoreApplication::translate("Utils::ForeignApps", "Failed to find Python executable. Path: \"%1\".")
.arg(preferredPythonPath), Log::WARNING);
}
else
{
// auto detect only when there are no preferred python path
if (!pyInfo.isValid())
{
if (testPythonInstallation(u"python3"_s, pyInfo))
return pyInfo;
LogMsg(QCoreApplication::translate("Utils::ForeignApps", "Failed to find `python3` executable in PATH environment variable. PATH: \"%1\"")
.arg(qEnvironmentVariable("PATH")), Log::INFO);
if (testPythonInstallation(u"python"_s, pyInfo))
return pyInfo;
LogMsg(QCoreApplication::translate("Utils::ForeignApps", "Failed to find `python` executable in PATH environment variable. PATH: \"%1\"")
.arg(qEnvironmentVariable("PATH")), Log::INFO);
#if defined(Q_OS_WIN)
if (testPythonInstallation(findPythonPath(), pyInfo))
return pyInfo;
if (testPythonInstallation(findPythonPath(), pyInfo))
return pyInfo;
LogMsg(QCoreApplication::translate("Utils::ForeignApps", "Failed to find `python` executable in Windows Registry."), Log::INFO);
#endif
LogMsg(QCoreApplication::translate("Utils::ForeignApps", "Python not detected"), Log::INFO);
LogMsg(QCoreApplication::translate("Utils::ForeignApps", "Failed to find Python executable"), Log::WARNING);
}
}
return pyInfo;