diff --git a/jedi/api/environment.py b/jedi/api/environment.py index 5dac55c2..38e21f1c 100644 --- a/jedi/api/environment.py +++ b/jedi/api/environment.py @@ -153,9 +153,9 @@ def _get_virtual_env_from_var(): variable is considered to be safe / controlled by the user solely. """ var = os.environ.get('VIRTUAL_ENV') - if var is not None: + if var: if var == sys.prefix: - return SameEnvironment() + return _try_get_same_env() try: return create_environment(var, safe=False) @@ -184,6 +184,10 @@ def get_default_environment(): if virtual_env is not None: return virtual_env + return _try_get_same_env() + + +def _try_get_same_env(): env = SameEnvironment() if not os.path.basename(env.executable).lower().startswith('python'): # This tries to counter issues with embedding. In some cases (e.g. diff --git a/test/test_api/test_environment.py b/test/test_api/test_environment.py index 834f0ebd..cdbe4051 100644 --- a/test/test_api/test_environment.py +++ b/test/test_api/test_environment.py @@ -131,11 +131,13 @@ def test_get_default_environment_from_env_does_not_use_safe(tmpdir, monkeypatch) assert env.path == 'fake' -def test_get_default_environment_when_embedded(monkeypatch): +@pytest.mark.parametrize('virtualenv', ['', 'fufuuuuu', sys.prefix]) +def test_get_default_environment_when_embedded(monkeypatch, virtualenv): # When using Python embedded, sometimes the executable is not a Python # executable. executable_name = 'RANDOM_EXE' monkeypatch.setattr(sys, 'executable', executable_name) + monkeypatch.setenv('VIRTUAL_ENV', virtualenv) env = get_default_environment() assert env.executable != executable_name