-
Notifications
You must be signed in to change notification settings - Fork 414
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
Support running with the Windows embeddable Python distribution #465
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import shutil | ||
import subprocess | ||
import sys | ||
|
||
from pipx.constants import WINDOWS | ||
from pipx.util import PipxError | ||
|
||
|
||
def has_venv() -> bool: | ||
try: | ||
import venv # noqa | ||
|
||
return True | ||
except ImportError: | ||
return False | ||
|
||
|
||
# If we are running under the Windows embeddable distribution, | ||
# venv isn't available (and we probably don't want to use the | ||
# embeddable distribution as our applications' base Python anyway) | ||
# so we try to locate the system Python and use that instead. | ||
|
||
# This code was copied from https://github.com/uranusjr/pipx-standalone | ||
# which uses this technique to build a completely standalone pipx | ||
# distribution | ||
|
||
|
||
def _find_default_windows_python() -> str: | ||
|
||
if has_venv(): | ||
return sys.executable | ||
|
||
py = shutil.which("py") | ||
if py: | ||
return py | ||
python = shutil.which("python") | ||
if python is None: | ||
raise PipxError("no suitable Python found") | ||
|
||
if "WindowsApps" not in python: | ||
return python | ||
# Special treatment to detect Windows Store stub. | ||
# https://twitter.com/zooba/status/1212454929379581952 | ||
|
||
proc = subprocess.run( | ||
[python, "-V"], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, | ||
) | ||
if proc.returncode != 0: | ||
# Cover the 9009 return code pre-emptively. | ||
raise PipxError("no suitable Python found") | ||
if not proc.stdout.strip(): | ||
# A real Python should print version, Windows Store stub won't. | ||
raise PipxError("no suitable Python found") | ||
return python # This executable seems to work. | ||
|
||
|
||
if WINDOWS: | ||
DEFAULT_PYTHON = _find_default_windows_python() | ||
else: | ||
DEFAULT_PYTHON = sys.executable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import shutil | ||
import subprocess | ||
import sys | ||
import pytest # type: ignore | ||
import pipx.interpreter | ||
from pipx.interpreter import _find_default_windows_python | ||
from pipx.util import PipxError | ||
|
||
|
||
def test_windows_python_venv_present(monkeypatch): | ||
monkeypatch.setattr(pipx.interpreter, "has_venv", lambda: True) | ||
assert _find_default_windows_python() == sys.executable | ||
|
||
|
||
def test_windows_python_no_venv_py_present(monkeypatch): | ||
def which(name): | ||
if name == "py": | ||
return "py" | ||
|
||
monkeypatch.setattr(pipx.interpreter, "has_venv", lambda: False) | ||
monkeypatch.setattr(shutil, "which", which) | ||
assert _find_default_windows_python() == "py" | ||
|
||
|
||
def test_windows_python_no_venv_python_present(monkeypatch): | ||
def which(name): | ||
if name == "python": | ||
return "python" | ||
# Note: returns False for "py" | ||
|
||
monkeypatch.setattr(pipx.interpreter, "has_venv", lambda: False) | ||
monkeypatch.setattr(shutil, "which", which) | ||
assert _find_default_windows_python() == "python" | ||
|
||
|
||
def test_windows_python_no_venv_no_python(monkeypatch): | ||
def which(name): | ||
return None | ||
|
||
monkeypatch.setattr(pipx.interpreter, "has_venv", lambda: False) | ||
monkeypatch.setattr(shutil, "which", which) | ||
with pytest.raises(PipxError): | ||
_find_default_windows_python() | ||
|
||
|
||
# Test the checks for the store Python. | ||
def test_windows_python_no_venv_store_python(monkeypatch): | ||
def which(name): | ||
if name == "python": | ||
return "WindowsApps" | ||
|
||
class dummy_runner: | ||
def __init__(self, rc, out): | ||
self.rc = rc | ||
self.out = out | ||
|
||
def __call__(self, *args, **kw): | ||
class Ret: | ||
pass | ||
|
||
ret = Ret() | ||
ret.returncode = self.rc | ||
ret.stdout = self.out | ||
return ret | ||
|
||
monkeypatch.setattr(pipx.interpreter, "has_venv", lambda: False) | ||
monkeypatch.setattr(shutil, "which", which) | ||
|
||
# Store version stub gives return code 9009 | ||
monkeypatch.setattr(subprocess, "run", dummy_runner(9009, "")) | ||
with pytest.raises(PipxError): | ||
_find_default_windows_python() | ||
|
||
# Even if it doesn't, it returns no output | ||
monkeypatch.setattr(subprocess, "run", dummy_runner(0, "")) | ||
with pytest.raises(PipxError): | ||
_find_default_windows_python() | ||
|
||
# If it *does* pass the tests, we use it as it's not the stub | ||
monkeypatch.setattr(subprocess, "run", dummy_runner(0, "3.8")) | ||
assert _find_default_windows_python() == "WindowsApps" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do these comment blocks belong inside the function below instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was intentional to put the comments before the function rather than inside it (I find it more readable that way). But I can reword it a bit, I guess. Maybe swap the paragraphs, and say "The following code was copied from ... which uses the same technique..."?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the paragraphs seem more natural to me if swapped.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.