Python: prefer python from path when detecting python for document

Change-Id: I794a741fa7257833f0b4efbc25dfae43b8748427
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2022-06-09 15:29:11 +02:00
parent 02bfd03c22
commit 2b428d5de0

View File

@@ -47,18 +47,20 @@ namespace Internal {
FilePath detectPython(const FilePath &documentPath) FilePath detectPython(const FilePath &documentPath)
{ {
PythonProject *project = documentPath.isEmpty() Project *project = documentPath.isEmpty() ? nullptr
? nullptr : SessionManager::projectForFile(documentPath);
: qobject_cast<PythonProject *>(
SessionManager::projectForFile(documentPath));
if (!project) if (!project)
project = qobject_cast<PythonProject *>(SessionManager::startupProject()); project = SessionManager::startupProject();
Environment env = Environment::systemEnvironment();
if (project) { if (project) {
if (auto target = project->activeTarget()) { if (auto target = project->activeTarget()) {
if (auto runConfig = target->activeRunConfiguration()) { if (auto runConfig = target->activeRunConfiguration()) {
if (auto interpreter = runConfig->aspect<InterpreterAspect>()) if (auto interpreter = runConfig->aspect<InterpreterAspect>())
return interpreter->currentInterpreter().command; return interpreter->currentInterpreter().command;
if (auto environmentAspect = runConfig->aspect<EnvironmentAspect>())
env = environmentAspect->environment();
} }
} }
} }
@@ -72,6 +74,14 @@ FilePath detectPython(const FilePath &documentPath)
if (defaultInterpreter.exists()) if (defaultInterpreter.exists())
return defaultInterpreter; return defaultInterpreter;
const FilePath python3FromPath = env.searchInPath("python3");
if (python3FromPath.exists())
return python3FromPath;
const FilePath pythonFromPath = env.searchInPath("python");
if (pythonFromPath.exists())
return pythonFromPath;
return PythonSettings::interpreters().value(0).command; return PythonSettings::interpreters().value(0).command;
} }