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

Make Python command absolute before calling subprocess #300

Merged

Conversation

uranusjr
Copy link
Member

bpo-38905

On Python 3.7.2 or later, sys.executable returns the wrong executable inside venv on Windows unless you invoke the interpreter with an absolute path. It seems that the behaviour is expected and unlikely to be fixed (at least not any time soon). Core devs seem to approve the shutil.which() workaround.

This works around bpo-38905 with suggestion from Steve Dower.

https://bugs.python.org/issue38905
@takluyver
Copy link
Member

That's rather frustrating. I've worked on a bunch of code that relies on sys.executable giving me an absolute, reliable path, regardless of how Python was invoked.

If we're using which() to get an absolute path anyway, does then finding sys.executable ever make any difference?

@takluyver
Copy link
Member

I was just about to post a grumpy message to that Python issue, but on reading it more carefully, I think I see it's not sys.executable that is giving you the wrong result, but the subprocess launch that's doing something different from a normal PATH search. That's less concerning; I'm OK with doing a which() on it in Python.

flit/__init__.py Outdated
@@ -22,6 +23,7 @@ def find_python_executable(python):
return sys.executable
if os.path.isabs(python): # sys.executable is absolute too
return python
python = shutil.which(python) or os.path.abspath(python)
Copy link
Member

Choose a reason for hiding this comment

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

Why the abspath call? I think which() already handles relative paths.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was not aware of this, thanks! TIL 😄

Copy link
Member Author

@uranusjr uranusjr Nov 27, 2019

Choose a reason for hiding this comment

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

What should we fall back to if the which call fails though? (It returns None if there’s no matching executable found.) Just the unresolved python? Because otherwise subprocess.check_call would fail with a cryptic TypeError.

Copy link
Member

Choose a reason for hiding this comment

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

We should probably throw an error, I'd guess - that means you've given it a name which isn't on PATH, or a path which doesn't exist or isn't executable.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added an exception for this.

@takluyver
Copy link
Member

Thanks, this LGTM.

@takluyver takluyver merged commit 46173cb into pypa:master Nov 27, 2019
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.

None yet

2 participants