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

Use venv API to get venv paths #907

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion src/pipx/shared_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
class _SharedLibs:
def __init__(self) -> None:
self.root = constants.PIPX_SHARED_LIBS
self.bin_path, self.python_path = get_venv_paths(self.root)
self.bin_path, self.python_path = get_venv_paths(DEFAULT_PYTHON, self.root)
self.pip_path = self.bin_path / ("pip" if not WINDOWS else "pip.exe")
# i.e. bin_path is ~/.local/pipx/shared/bin
# i.e. python_path is ~/.local/pipx/shared/python
Expand Down
33 changes: 20 additions & 13 deletions src/pipx/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,26 @@ def run_pypackage_bin(bin_path: Path, args: List[str]) -> NoReturn:
)


if WINDOWS:

def get_venv_paths(root: Path) -> Tuple[Path, Path]:
bin_path = root / "Scripts"
python_path = bin_path / "python.exe"
return bin_path, python_path

else:

def get_venv_paths(root: Path) -> Tuple[Path, Path]:
bin_path = root / "bin"
python_path = bin_path / "python"
return bin_path, python_path
def get_venv_paths(python: str, root: Path) -> Tuple[Path, Path]:
command_str = textwrap.dedent(
f"""
import venv
root_paths = venv.EnvBuilder().ensure_directories(r"{str(root)}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
root_paths = venv.EnvBuilder().ensure_directories(r"{str(root)}")
root_paths = venv.EnvBuilder().ensure_directories({root})

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably raw string should be used? (For handling paths on Windows)

print(root_paths.bin_path + ";" + root_paths.env_exe)
"""
)
output = (
run_subprocess(
[python, "-c", command_str],
capture_stderr=False,
log_cmd_str="<get_venv_paths commands>",
)
.stdout.strip()
.split(";")
)
bin_path = Path(output[0])
python_path = Path(output[1])
return bin_path, python_path


def get_site_packages(python: Path) -> Path:
Expand Down
2 changes: 1 addition & 1 deletion src/pipx/venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def __init__(
) -> None:
self.root = path
self.python = python
self.bin_path, self.python_path = get_venv_paths(self.root)
self.bin_path, self.python_path = get_venv_paths(python, self.root)
self.pipx_metadata = PipxMetadata(venv_dir=path)
self.verbose = verbose
self.do_animation = not verbose
Expand Down
6 changes: 4 additions & 2 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from packaging.utils import canonicalize_name

from package_info import PKG
from pipx import constants, main, pipx_metadata_file, util
from pipx import constants, interpreter, main, pipx_metadata_file, util

WIN = sys.platform.startswith("win")

Expand Down Expand Up @@ -168,7 +168,9 @@ def assert_package_metadata(test_metadata, ref_metadata):


def remove_venv_interpreter(venv_name):
_, venv_python_path = util.get_venv_paths(constants.PIPX_LOCAL_VENVS / venv_name)
_, venv_python_path = util.get_venv_paths(
interpreter.DEFAULT_PYTHON, constants.PIPX_LOCAL_VENVS / venv_name
)
assert venv_python_path.is_file()
venv_python_path.unlink()
assert not venv_python_path.is_file()