@@ -226,14 +226,22 @@ def jedi_names(self, all_scopes=False, definitions=True, references=False):
226226 def jedi_script (self , position = None ):
227227 extra_paths = []
228228 environment_path = None
229+ env_vars = None
229230
230231 if self ._config :
231232 jedi_settings = self ._config .plugin_settings ('jedi' , document_path = self .path )
232233 environment_path = jedi_settings .get ('environment' )
233234 extra_paths = jedi_settings .get ('extra_paths' ) or []
235+ env_vars = jedi_settings .get ('env_vars' )
234236
235- environment = self .get_enviroment (environment_path ) if environment_path else None
236- sys_path = self .sys_path (environment_path ) + extra_paths
237+ # Drop PYTHONPATH from env_vars before creating the environment because that makes
238+ # Jedi throw an error.
239+ if env_vars is None :
240+ env_vars = os .environ .copy ()
241+ env_vars .pop ('PYTHONPATH' , None )
242+
243+ environment = self .get_enviroment (environment_path , env_vars = env_vars ) if environment_path else None
244+ sys_path = self .sys_path (environment_path , env_vars = env_vars ) + extra_paths
237245 project_path = self ._workspace .root_path
238246
239247 kwargs = {
@@ -249,22 +257,25 @@ def jedi_script(self, position=None):
249257
250258 return jedi .Script (** kwargs )
251259
252- def get_enviroment (self , environment_path = None ):
260+ def get_enviroment (self , environment_path = None , env_vars = None ):
253261 # TODO(gatesn): #339 - make better use of jedi environments, they seem pretty powerful
254262 if environment_path is None :
255263 environment = jedi .api .environment .get_cached_default_environment ()
256264 else :
257265 if environment_path in self ._workspace ._environments :
258266 environment = self ._workspace ._environments [environment_path ]
259267 else :
260- environment = jedi .api .environment .create_environment (path = environment_path , safe = False )
268+ environment = jedi .api .environment .create_environment (path = environment_path ,
269+ safe = False ,
270+ env_vars = env_vars )
261271 self ._workspace ._environments [environment_path ] = environment
262272
263273 return environment
264274
265- def sys_path (self , environment_path = None ):
275+ def sys_path (self , environment_path = None , env_vars = None ):
266276 # Copy our extra sys path
277+ # TODO: when safe to break API, use env_vars explicitly to pass to create_environment
267278 path = list (self ._extra_sys_path )
268- environment = self .get_enviroment (environment_path = environment_path )
279+ environment = self .get_enviroment (environment_path = environment_path , env_vars = env_vars )
269280 path .extend (environment .get_sys_path ())
270281 return path
0 commit comments