Skip to content

Make Python command absolute before calling subprocess#300

Merged
takluyver merged 2 commits into
pypa:masterfrom
uranusjr:resolve-python-abspath-before-subprocess
Nov 27, 2019
Merged

Make Python command absolute before calling subprocess#300
takluyver merged 2 commits into
pypa:masterfrom
uranusjr:resolve-python-abspath-before-subprocess

Conversation

@uranusjr

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread flit/__init__.py Outdated
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
Copy Markdown
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
Copy Markdown
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 😄

@uranusjr uranusjr Nov 27, 2019

Copy link
Copy Markdown
Member Author

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

2 participants