Skip to content

Commit

Permalink
* add site-packages to jedi configuration; see davidhalter/jedi#1540
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Clary committed Apr 11, 2020
1 parent 6fc147d commit 75e8a83
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
4 changes: 2 additions & 2 deletions spyder/app/tests/test_mainwindow.py
Expand Up @@ -55,7 +55,7 @@
from spyder.plugins.help.tests.test_plugin import check_text
from spyder.plugins.ipythonconsole.utils.kernelspec import SpyderKernelSpec
from spyder.py3compat import PY2, to_text_string
from spyder.utils.programs import is_module_installed
from spyder.utils.programs import get_python_site_packages
from spyder.widgets.dock import DockTitleBar
from spyder.utils.misc import remove_backslashes

Expand Down Expand Up @@ -2447,7 +2447,7 @@ def test_preferences_change_interpreter(qtbot, main_window):
config = lsp.generate_python_config()
jedi = config['configurations']['pyls']['plugins']['jedi']
assert jedi['environment'] == sys.executable
assert jedi['extra_paths'] == []
assert jedi['extra_paths'] == get_python_site_packages(sys.executable)


@pytest.mark.slow
Expand Down
10 changes: 8 additions & 2 deletions spyder/plugins/completion/languageserver/plugin.py
Expand Up @@ -26,6 +26,7 @@
from spyder.config.manager import CONF
from spyder.api.completion import SpyderCompletionPlugin
from spyder.utils.misc import check_connection_port, getcwd_or_home
from spyder.utils.programs import get_python_site_packages
from spyder.plugins.completion.languageserver import LSP_LANGUAGES
from spyder.plugins.completion.languageserver.client import LSPClient
from spyder.plugins.completion.languageserver.confpage import (
Expand Down Expand Up @@ -574,15 +575,20 @@ def generate_python_config(self):
}

# Jedi configuration
extra_paths = self.get_option('spyder_pythonpath', section='main',
default=[])
if self.get_option('default', section='main_interpreter'):
environment = None
else:
environment = self.get_option('custom_interpreter',
section='main_interpreter')
# jedi can miss the site-package: see
# https://github.com/davidhalter/jedi/issues/1540
extra_paths += get_python_site_packages(environment)

jedi = {
'environment': environment,
'extra_paths': self.get_option('spyder_pythonpath',
section='main', default=[]),
'extra_paths': extra_paths,
}
jedi_completion = {
'enabled': self.get_option('code_completion'),
Expand Down
14 changes: 14 additions & 0 deletions spyder/utils/programs.py
Expand Up @@ -24,6 +24,7 @@
import tempfile
import threading
import time
import ast

# Third party imports
import psutil
Expand Down Expand Up @@ -967,6 +968,19 @@ def check_python_help(filename):
return False


def get_python_site_packages(pyexec):
"""Get the site-packages for a given python executable"""
script = 'import site; print(site.getsitepackages())'
try:
proc = run_program('env', ['-i', pyexec, '-c', script])
stdout, stderr = proc.communicate()
pkgs = ast.literal_eval(stdout.decode())
except Exception:
pkgs = []

return pkgs


def is_spyder_process(pid):
"""
Test whether given PID belongs to a Spyder process.
Expand Down

0 comments on commit 75e8a83

Please sign in to comment.