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

Conversation

dukecat0
Copy link
Member

  • I have added an entry to docs/changelog.md

Summary of changes

Use venv API to get venv paths.

Closes #701

Test plan

Tested by running

pipx install black --verbose

@dukecat0
Copy link
Member Author

However, some users might not have venv installed (see #853). Maybe we should also support virtualenv?

Copy link
Member

@uranusjr uranusjr left a comment

Choose a reason for hiding this comment

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

I have a gut feeling this may be problematic if the environment path contains non-ASCII characters, since not all terminals can output those characters (say if the encoding is not configured to UTF-8).

It’s probably more reliable to write the path to a temp file instead.

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)

@uranusjr
Copy link
Member

Maybe we should also support virtualenv?

Does virtualenv provide an interface to introspect an environment outside of it? Because an environment created by virtualenv does not have access to the virtualenv module.

@dukecat0
Copy link
Member Author

I have a gut feeling this may be problematic if the environment path contains non-ASCII characters, since not all terminals > can output those characters (say if the encoding is not configured to UTF-8).

It’s probably more reliable to write the path to a temp file instead.

pipx/src/pipx/util.py

Lines 130 to 137 in 4c0f289

def get_site_packages(python: Path) -> Path:
output = run_subprocess(
[python, "-c", "import sysconfig; print(sysconfig.get_path('purelib'))"],
capture_stderr=False,
).stdout
path = Path(output.strip())
path.mkdir(parents=True, exist_ok=True)
return path

The output is just printed to the terminal in this function and it seems fine?

@dukecat0
Copy link
Member Author

Does virtualenv provide an interface to introspect an environment outside of it?

I think no. Or probably we can keep this for virtualenv users?

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

@uranusjr
Copy link
Member

If we need to keep the bin/Script guess logic I’d rather keep it and improve the detection logic to account for MSYS. I think there’s established logic out there somewhere that can reliably detect this.

@dukecat0
Copy link
Member Author

I think there’s established logic out there somewhere that can reliably detect this.

Yes, for example python-poetry/poetry#4467 and python-poetry/poetry#3713.

@dukecat0
Copy link
Member Author

dukecat0 commented Oct 30, 2022

If we need to keep the bin/Script guess logic I’d rather keep it and improve the detection logic to account for MSYS.

I will open another PR for this. (#908)

@dukecat0 dukecat0 closed this Oct 30, 2022
@dukecat0 dukecat0 deleted the update-get-venv-paths branch October 30, 2022 06:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pipx enters in infinite loop when trying to install a package on python distributed with MSYS2 on Windows
2 participants