Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get fullpath of given python executable when activating env #5086

Merged
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
26 changes: 18 additions & 8 deletions src/poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,21 @@ class EnvManager:
def __init__(self, poetry: "Poetry") -> None:
self._poetry = poetry

def _full_python_path(self, python: str) -> str:
try:
executable = decode(
subprocess.check_output(
list_to_shell_command(
[python, "-c", '"import sys; print(sys.executable)"']
),
shell=True,
).strip()
)
except CalledProcessError as e:
raise EnvCommandError(e)

return executable

def _detect_active_python(self, io: "IO") -> str:
executable = None

Expand All @@ -474,14 +489,7 @@ def _detect_active_python(self, io: "IO") -> str:
" config.",
verbosity=Verbosity.VERBOSE,
)
executable = decode(
subprocess.check_output(
list_to_shell_command(
["python", "-c", '"import sys; print(sys.executable)"']
),
shell=True,
).strip()
)
executable = self._full_python_path("python")
io.write_line(f"Found: {executable}", verbosity=Verbosity.VERBOSE)
except CalledProcessError:
io.write_line(
Expand Down Expand Up @@ -511,6 +519,8 @@ def activate(self, python: str, io: "IO") -> "Env":
# Executable in PATH or full executable path
pass

python = self._full_python_path(python)

try:
python_version = decode(
subprocess.check_output(
Expand Down
2 changes: 2 additions & 0 deletions tests/console/commands/env/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def check_output(cmd: str, *_: Any, **__: Any) -> str:
return version.text
elif "sys.version_info[:2]" in cmd:
return f"{version.major}.{version.minor}"
elif '-c "import sys; print(sys.executable)"' in cmd:
return f"/usr/bin/{cmd.split()[0]}"
else:
return str(Path("/prefix"))

Expand Down
2 changes: 1 addition & 1 deletion tests/console/commands/env/test_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_activate_activates_non_existing_virtualenv_no_envs_file(
venv_py37 = venv_cache / f"{venv_name}-py3.7"
mock_build_env.assert_called_with(
venv_py37,
executable="python3.7",
executable="/usr/bin/python3.7",
flags={"always-copy": False, "system-site-packages": False},
with_pip=True,
with_setuptools=True,
Expand Down
14 changes: 8 additions & 6 deletions tests/utils/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,14 @@ def build_venv(path: Union[Path, str], **__: Any) -> None:

def check_output_wrapper(
version: Version = VERSION_3_7_1,
) -> Callable[[List[str], Any, Any], str]:
def check_output(cmd: List[str], *args: Any, **kwargs: Any) -> str:
) -> Callable[[str, Any, Any], str]:
def check_output(cmd: str, *args: Any, **kwargs: Any) -> str:
if "sys.version_info[:3]" in cmd:
return version.text
elif "sys.version_info[:2]" in cmd:
return f"{version.major}.{version.minor}"
elif '-c "import sys; print(sys.executable)"' in cmd:
return f"/usr/bin/{cmd.split()[0]}"
else:
return str(Path("/prefix"))

Expand Down Expand Up @@ -193,7 +195,7 @@ def test_activate_activates_non_existing_virtualenv_no_envs_file(

m.assert_called_with(
Path(tmp_dir) / f"{venv_name}-py3.7",
executable="python3.7",
executable="/usr/bin/python3.7",
flags={"always-copy": False, "system-site-packages": False},
with_pip=True,
with_setuptools=True,
Expand Down Expand Up @@ -328,7 +330,7 @@ def test_activate_activates_different_virtualenv_with_envs_file(

m.assert_called_with(
Path(tmp_dir) / f"{venv_name}-py3.6",
executable="python3.6",
executable="/usr/bin/python3.6",
flags={"always-copy": False, "system-site-packages": False},
with_pip=True,
with_setuptools=True,
Expand Down Expand Up @@ -389,7 +391,7 @@ def test_activate_activates_recreates_for_different_patch(

build_venv_m.assert_called_with(
Path(tmp_dir) / f"{venv_name}-py3.7",
executable="python3.7",
executable="/usr/bin/python3.7",
flags={"always-copy": False, "system-site-packages": False},
with_pip=True,
with_setuptools=True,
Expand Down Expand Up @@ -1022,7 +1024,7 @@ def test_activate_with_in_project_setting_does_not_fail_if_no_venvs_dir(

m.assert_called_with(
poetry.file.parent / ".venv",
executable="python3.7",
executable="/usr/bin/python3.7",
flags={"always-copy": False, "system-site-packages": False},
with_pip=True,
with_setuptools=True,
Expand Down