Repository: davidhalter/jedi

Issue #1142: get_default_environment can return a python3 environment when running on python2
Created: 2018-06-12T10:31:31Z
It doesn't seem correct that jedi.environment.get_default_environment can return a python3 environment even though jedi is running in python2?

Duplicate issue #1187: Demo code fails in Python 2.7 when Py3 is installed
Created: 2018-07-30T15:00:20Z
I used pip to install jedi in a Python 2.7 build, then tried the example code snippet in the docs. The result was the trace below; with some staring and bewilderment, I eventually realized that jedi was trying to run a totally different interpreter (not to mention interpreter version).
Stepping around a bit in the debugger, it looks like last time the relevant code, get_default_environment(), was touched is #1082
Specific problem: trying to run /usr/local/bin/python3.6 when started in a different interpreter (/opt/bld/s5nj/python-install/bin/python), with different (fully functional and correct) paths:
InternalError: The subprocess /usr/local/bin/python3.6 has crashed (EOFError(), stderr=Fatal Python error: Py_Initialize: unable to load the file system codec
  File "/opt/bld/s5nj/python-install/lib/python2.7/encodings/__init__.py", line 123
    raise CodecRegistryError,\
                            ^

Backtrace:
Python 2.7.13 (default, Apr  5 2018, 16:41:55)
Type "copyright", "credits" or "license" for more information.

IPython 5.7.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import jedi

In [2]: source = '''
   ...: import datetime
   ...: datetime.da'''

In [3]: script = jedi.Script(source, 3, len('datetime.da'), 'example.py')

In [4]: completions = script.completions()
---------------------------------------------------------------------------
InternalError                             Traceback (most recent call last)
<ipython-input-4-0c6611084d9e> in <module>()
----> 1 completions = script.completions()

/opt/bld/s5nj/python-install/lib/python2.7/site-packages/jedi/api/__init__.pyc in completions(self)
    169         debug.speed('completions start')
    170         completion = Completion(
--> 171             self._evaluator, self._get_module(), self._code_lines,
    172             self._pos, self.call_signatures
    173         )

/opt/bld/s5nj/python-install/lib/python2.7/site-packages/jedi/api/__init__.pyc in _get_module(self)
    145         name = '__main__'
    146         if self.path is not None:
--> 147             n = dotted_path_in_sys_path(self._evaluator.get_sys_path(), self.path)
    148             if n is not None:
    149                 name = n

/opt/bld/s5nj/python-install/lib/python2.7/site-packages/jedi/evaluate/__init__.pyc in get_sys_path(self)
    121     def get_sys_path(self):
    122         """Convenience function"""
--> 123         return self.project._get_sys_path(self, environment=self.environment)
    124
    125     def eval_element(self, context, element):

/opt/bld/s5nj/python-install/lib/python2.7/site-packages/jedi/evaluate/cache.pyc in wrapper(obj, *args, **kwargs)
     38                 if default is not _NO_DEFAULT:
     39                     memo[key] = default
---> 40                 rv = function(obj, *args, **kwargs)
     41                 memo[key] = rv
     42                 return rv

/opt/bld/s5nj/python-install/lib/python2.7/site-packages/jedi/api/project.pyc in _get_sys_path(self, evaluator, environment)
    102         prefixed = []
    103
--> 104         sys_path = list(self._get_base_sys_path(environment))
    105         if self._smart_sys_path:
    106             prefixed.append(self._path)

/opt/bld/s5nj/python-install/lib/python2.7/site-packages/jedi/api/project.pyc in _get_base_sys_path(self, environment)
     86             environment = self.get_environment()
     87
---> 88         sys_path = environment.get_sys_path()
     89         try:
     90             sys_path.remove('')

/opt/bld/s5nj/python-install/lib/python2.7/site-packages/jedi/cache.pyc in wrapper(self, *args, **kwargs)
    140             return dct[key]
    141         except KeyError:
--> 142             result = method(self, *args, **kwargs)
    143             dct[key] = result
    144             return result

/opt/bld/s5nj/python-install/lib/python2.7/site-packages/jedi/api/environment.pyc in get_sys_path(self)
    114         # If you omit -S when starting Python (normal case), additionally
    115         # site.py gets executed.
--> 116         return self._get_subprocess().get_sys_path()
    117
    118

/opt/bld/s5nj/python-install/lib/python2.7/site-packages/jedi/evaluate/compiled/subprocess/__init__.pyc in get_sys_path(self)
    167
    168     def get_sys_path(self):
--> 169         return self._send(None, functions.get_sys_path, (), {})
    170
    171     def kill(self):

/opt/bld/s5nj/python-install/lib/python2.7/site-packages/jedi/evaluate/compiled/subprocess/__init__.pyc in _send(self, evaluator_id, function, args, kwargs)
    219                     self._executable,
    220                     eof_error,
--> 221                     stderr,
    222                 ))
    223

InternalError: The subprocess /usr/local/bin/python3.6 has crashed (EOFError(), stderr=Fatal Python error: Py_Initialize: unable to load the file system codec
  File "/opt/bld/s5nj/python-install/lib/python2.7/encodings/__init__.py", line 123
    raise CodecRegistryError,\
                            ^
SyntaxError: invalid syntax

Current thread 0x00007fffa89213c0 (most recent call first):
).

Closing comment (2018-07-30T15:28:49Z): I guess this is technically a dup of #1142