Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions pyls/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,22 @@ def jedi_names(self, all_scopes=False, definitions=True, references=False):
def jedi_script(self, position=None):
extra_paths = []
environment_path = None
env_vars = None

if self._config:
jedi_settings = self._config.plugin_settings('jedi', document_path=self.path)
environment_path = jedi_settings.get('environment')
extra_paths = jedi_settings.get('extra_paths') or []
env_vars = jedi_settings.get('env_vars')

environment = self.get_enviroment(environment_path) if environment_path else None
sys_path = self.sys_path(environment_path) + extra_paths
# Drop PYTHONPATH from env_vars before creating the environment because that makes
# Jedi throw an error.
if env_vars is None:
env_vars = os.environ.copy()
env_vars.pop('PYTHONPATH', None)

environment = self.get_enviroment(environment_path, env_vars=env_vars) if environment_path else None
sys_path = self.sys_path(environment_path, env_vars=env_vars) + extra_paths
project_path = self._workspace.root_path

kwargs = {
Expand All @@ -249,22 +257,25 @@ def jedi_script(self, position=None):

return jedi.Script(**kwargs)

def get_enviroment(self, environment_path=None):
def get_enviroment(self, environment_path=None, env_vars=None):
# TODO(gatesn): #339 - make better use of jedi environments, they seem pretty powerful
if environment_path is None:
environment = jedi.api.environment.get_cached_default_environment()
else:
if environment_path in self._workspace._environments:
environment = self._workspace._environments[environment_path]
else:
environment = jedi.api.environment.create_environment(path=environment_path, safe=False)
environment = jedi.api.environment.create_environment(path=environment_path,
safe=False,
env_vars=env_vars)
self._workspace._environments[environment_path] = environment

return environment

def sys_path(self, environment_path=None):
def sys_path(self, environment_path=None, env_vars=None):
# Copy our extra sys path
# TODO: when safe to break API, use env_vars explicitly to pass to create_environment
path = list(self._extra_sys_path)
environment = self.get_enviroment(environment_path=environment_path)
environment = self.get_enviroment(environment_path=environment_path, env_vars=env_vars)
path.extend(environment.get_sys_path())
return path
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'configparser; python_version<"3.0"',
'future>=0.14.0; python_version<"3"',
'backports.functools_lru_cache; python_version<"3.2"',
'jedi>=0.17.0,<0.18.0',
'jedi>=0.17.2,<0.18.0',
'python-jsonrpc-server>=0.4.0',
'pluggy',
'ujson<=2.0.3 ; platform_system!="Windows" and python_version<"3.0"',
Expand Down
5 changes: 5 additions & 0 deletions vscode-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
"default": [],
"description": "Define extra paths for jedi.Script."
},
"pyls.plugins.jedi.env_vars": {
"type": "dictionary",
"default": null,
"description": "Define environment variables for jedi.Script and Jedi.names."
},
"pyls.plugins.jedi.environment": {
"type": "string",
"default": null,
Expand Down