-
Notifications
You must be signed in to change notification settings - Fork 3k
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
pip3 need honor sys.executable in OS X #2031
Comments
I think this is wheel-specific. The desired behavior in Homebrew is that the shebang is set to the opt path, /usr/local/opt/python3/bin/python3.4, which does not change between micro versions of python (and not the Cellar path, /usr/local/Cellar/python3/3.4.1_1/Frameworks/Python.framework/Versions/3.4/bin/python3.4, which does). sys.executable is set to the opt path. Installing ipython with pip3 --no-use-wheel gives #!/usr/local/opt/python3/bin/python3.4 as the shebang; installing ipython from the wheel uses #!/usr/local/Cellar/python3/3.4.1_1/Frameworks/Python.framework/Versions/3.4/bin/python3.4 as the shebang. We would prefer that the wheel behavior matches the setup.py behavior. |
This is actually in the distlib project, or at least the linked code is. That's located at https://bitbucket.org/pypa/distlib. Perhaps @vsajip can jump in and give a rationale though. |
I would have been more inclined to blame fix_script in pip/wheel.py -- https://github.com/pypa/pip/blob/1.5.6/pip/wheel.py#L67-L88 -- except that I think it does what I want. Is there some reason python3 would be invoked without applying sitecustomize during a wheel install? |
Not unless pip itself is invoked without it. |
I think @xu-cheng was right; os.environ['PYENV_LAUNCHER'] takes the value of sys.argv[0] in python3; changing the shebang in the pip3 script changes the shebangs it writes for wheels. I wonder why this is wheel-specific! |
The reason for the logic in The environment variable is set by the stub launcher, and so won't be present if the interpreter in the framework build is invoked directly. @xu-cheng AFAIK, the executable pointed to by |
@vsajip As for my test shown, When using Homebrew we want the shebang set as |
@xu-cheng Unfortunately I am unable to build Python 3.4 on the somewhat aging Mac (Leopard) that I have access to. However, I do have a build which I made in January, and installed using
I printed
I know this is an old machine and an old Python version, but that shouldn't matter. From the above, you'll see that I'm also unfortunately unable to install a Python 3.4 on this machine from a |
I wonder why the second and third results of your test have different python version with your first one. Anyway, I installed python 3.3.0 on my Mac through pyenv (not confused with pyvenv). Here's the results:
So there's a chance that this issue is python 3.4 specific. |
You're right - I must have created the venvs from an older build. So I created some new venvs just now, and the results are different again!
Now, neither of them has a So, possibly some 3.3 versions are also affected. |
So the results got weird. |
Believe me, I didn't add this little complication for fun :-) I think it may depend on exactly how Python is built. When I implemented venv, I had to modify the stub launcher on OS X so that the stub value was used. I'm pretty sure that I don't claim to be a Python-on-OS X expert, and I'm not sure if anything has changed in the way Python is built on OS X or whether Homebrew builds differ from What happens with a non-Homebrew Python 3.4 (e.g. from a |
I just tested python 3.4.0 and 3.4.1 installed by pyenv. And it's surprise that neither of them have |
FYI: In the 3.4 sources,
Note that it is only set in one place: the stub launcher in IIUC, the stub launcher is only used in framework builds anyway, to allow GUI apps to work correctly. I'm not sure if the pyenv builds you mention are such. Here's an example using a Python 3.2 framework build:
However, what is
So, the stub is at |
Now it makes much more sense. The pyenv builds don't have framework enable, while homebrew bulids have. For homebrewed python, both If I use So now the problem is how to make |
Yes, but that is correct behaviour and needed for correct operation in venvs created by When |
My current problem has no concern with venvs. It's that homebrew uses stub launcher, therefore making But I think this problem would be spread to any framework enabled builds. I don't know whether I understand correctly. Shouldn't the correct code look similar like this: def get_executable():
if sys.platform == 'darwin' and \
('__PYVENV_LAUNCHER__' in os.environ) and \
venv_is_truly_running():
result = os.environ['__PYVENV_LAUNCHER__']
else:
result = sys.executable
return result |
The code that is determining the shebang to use is running in an installer (like The actual logic that
So, note that |
Whoops, I think I misread the code I mentioned above (read it after coming in from a late night :-) Let me get some coffee and look at it again... |
Okay, I've had some coffee now, let's see if I can make a little more sense.
ISTM it's worth checking why the environment variable is being set wrongly, and that means looking at the stub launcher code or the launcher invocation, rather than |
Yeah you're right. So here're the test I run to determine different executes' realpath and
|
In addition, the pip3 of homebrewed python is installed by command: |
So, if instead the |
Test shows it's not helping. Still wrong shebang. But I don't think we should solve the issue by finding right invoked command. From my understanding, the desired behavior is like this:
So which command is invoked shouldn't be the issue. |
The behaviour in If
Also, in |
As the test shown, invoking And |
Why is Since I can't run Homebrew Python on my system, I was hoping you could e.g. set a break point in |
I added a call to pdb.set_trace() inside get_executable() in /usr/local/lib/python3.4/site-packages/pip/_vendor/distlib/utils.py.
|
Oops, that was dumb, sorry; the same is true without the extra "python3 -m pdb" wrapper.
|
But then it says "It is nice to have the directory name as a cleaned up absolute path though, therefore call realpath on dirname(path)." Isn't that what's hosing us? |
Perhaps. If so, it would need to be a change in Python itself. Why is this not an issue for stock ( |
@vsajip The reason that I think As for your unable testing homebrew on your Leopard, can you try test with tigerbrew? EDIT sorry for copy/paste error. |
OK, I see. Unfortunately, my machine is a bit temperamental - although it has MacPorts installed and a whole bunch of MacPorts libraries, I still regularly get problems trying to install or upgrade stuff. So I'm wary of making any changes which might break something I need :-( Possibly @tdsmith has identified where the symlink resolution occurs, but that would mean a change to Python itself (not |
I can set up the python.org build but what is it we'd like to test? Homebrew doesn't patch Python and this manifestation is particular to the way Homebrew distributes python. Applying this patch against pythonw.c during the Homebrew build does indeed get rid of the symlink resolution so that pip takes a shebang that matches the ensurepip invocation and writes shebangs that match its own: https://gist.github.com/tdsmith/f6f3ab95101baf5de5fc I guess our next step is to open an issue against Python to see if we can convince them to change this behavior? I really appreciate your patience; this has been very helpful. |
While it was I that implemented the relevant change in |
I'm not a homebrew user so I don't have a setup to test this at the moment. But, if I understand from the description above, everything works correctly in the non-wheel case, (which I assume means that Distutils build_scripts.py is doing the shebang modification?) and not in the wheel case when distlib is used to do it. So, before we think about changing the stub launcher, it seems to me a first issue to resolve is why does the behavior of distlib differs from that of Distutils, which is after all the standard? |
It's likely to actually be setuptools doing it. Probably setuptools just uses |
Added this to the 6.0 milestone and I've emailed @vsajip asking for a new release of distlib. Hopefully that comes out in time for 6.0 but if not I think we can work around it. |
I found that pip3 from homebrew handles the shebang of installed scripts in wrong way(Homebrew/legacy-homebrew#32254). After further digging, I'm pretty sure it's pip's issue rather than homebrew.
The related code is in here:
I don't know why you guys would overwrite
sys.executable
. But as the test showed, under OS X python3__PYVENV_LAUNCHER__
will always exist.The text was updated successfully, but these errors were encountered: