From 6664d78ded334264c3486c0b9472b23f8f3fc2e0 Mon Sep 17 00:00:00 2001 From: David Schulz Date: Fri, 25 Oct 2019 09:15:59 +0200 Subject: Python: detect virtual environments for documents and projects After opening a document or project the directory hierarchy is looked up for a Scripts/(activate && python.exe) on windows or bin/(activate && python) on unix. This is the usual structure of python virtual environments. If such a folder is found add the python from that folder to the list of configured interpreters in the settings, set it as the current interpreter for the project and try to open the corresponding language server. Change-Id: I038c309ea2988f9370194330d250d1515beac0a0 Reviewed-by: Christian Stenger --- src/plugins/python/pythonutils.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'src/plugins/python/pythonutils.cpp') diff --git a/src/plugins/python/pythonutils.cpp b/src/plugins/python/pythonutils.cpp index 86ee34e922..aa7051b396 100644 --- a/src/plugins/python/pythonutils.cpp +++ b/src/plugins/python/pythonutils.cpp @@ -175,6 +175,11 @@ static FilePath detectPython(const FilePath &documentPath) } } + // check whether this file is inside a python virtual environment + QList venvInterpreters = PythonSettings::detectPythonVenvs(documentPath); + if (!python.exists()) + python = venvInterpreters.value(0).command; + if (!python.exists()) python = PythonSettings::defaultInterpreter().command; @@ -248,7 +253,13 @@ public: ? QString{"python-language-server[pyflakes]"} : QString{"python-language-server[all]"}; - m_process.start(m_python.toString(), {"-m", "pip", "install", "--user", pylsVersion}); + QStringList arguments = {"-m", "pip", "install", pylsVersion}; + + // add --user to global pythons, but skip it for venv pythons + if (!QDir(m_python.parentDir().toString()).exists("activate")) + arguments << "--user"; + + m_process.start(m_python.toString(), arguments); Core::MessageManager::write(tr("Running \"%1 %2\" to install Python language server") .arg(m_process.program(), m_process.arguments().join(' '))); -- cgit v1.2.1