@ilovezfs Cool. As I said I'm confused about Dom's reverse issue since `:python3` explicitly looks for `python3`. The intermediate variables should most likely reveal what's going on.
> And presumably you do not have a PYTHONPATH set manually, yes?
No, I don't.
I'd imagine a reproducer could simply be:
```sh
brew install python python3
mkdir -p /tmp/debug-brew-python
ln -s /usr/local/bin/python3 /tmp/debug-brew-python/python
PATH=/tmp/debug-brew-python:$PATH
unset PYTHONPATH # probably shouldn't matter; didn't check
brew install -s ... # some formula that depends_on => :python and has python resources, e.g., ipython@5
```
Let me know if that doesn't cut it.
For me, I have `~/.pyenv/versions/3.6.1/bin` in my `PATH`, which provides the first `python`. I also have both brewed `python` and `python3`. (Update: I don't typically have `PYTHONPATH` set when I run brew.) The diagnostics in my original post should explain this scenario fairly clearly; the question is how to fix it.
I'm confused about Dom's scenario though, and printing out some intermediate variables should shed some light.
Oh I just realized I was running into a py3x site-packages dir for py2x problem, while Dom ran into the opposite py2x site-packages dir for py3x problem. That's kinda funny, I'm slightly confused as to how he managed to get a 2.7 short version when `python_binary` is explicitly `"python3"`. Anyway, I don't have the time to investigate now.
PythonRequirement woes: Your PYTHONPATH points to a site-packages dir for Python 3.x but you are running Python 2.x
===================================================================================================================
See https://github.com/Homebrew/brew/pull/2917#issuecomment-317272358. I ran into this today myself, even with `HOMEBREW_ENV_FILTERING` turned on:
```
==> python -c import setuptools... --no-user-cfg install --prefix=/usr/local/Cellar/gnuradio/3.7.11_1/libexec/vendor --single-version-externally-managed --record=installed.txt
Your PYTHONPATH points to a site-packages dir for Python 3.x but you are running Python 2.x!
PYTHONPATH is currently: "/usr/local/Cellar/gnuradio/3.7.11_1/libexec/vendor/lib/python2.7/site-packages:/usr/local/lib/python3.6/site-packages"
You should `unset PYTHONPATH` to fix this.
```
## Reproducers
- site-packages dir for Python 3.x but you are running Python 2.x: https://github.com/Homebrew/brew/issues/2958#issuecomment-318661866
- site-packages dir for Python 2.x but you are running Python 3.x: https://github.com/Homebrew/brew/issues/2958#issuecomment-318917264
## Diagnostics
I looked into `python_requirement.rb` and apparently it's picking up a wrong `python_short_version` **when you have python3 as the default python on your PATH**, which in turn inserts the wrong site-packages path into `PYTHONPATH`:
```rb
ENV["PYTHONPATH"] = "#{HOMEBREW_PREFIX}/lib/python#{short_version}/site-packages"
```
Note that `python_short_version` is decided from `which_python`, which in turn is basically `which python_binary`, where `python_binary` is `"python"`; and `which` is defined in `requirement.rb`:
```rb
def which(cmd)
super(cmd, PATH.new(ORIGINAL_PATHS))
end
```
You immediately see what's wrong here: it's looking inside `ORIGINAL_PATHS`, and no wonder `HOMEBREW_ENV_FILTERING` doesn't help a bit.
## Resolution
I'm on a tight deadline for something else so I stopped my investigation there — I didn't do a bisect, and nor did I really think the problem through. However, it seems that since we're doing
```rb
# Homebrew Python should take precedence over other Pythons in the PATH
ENV.prepend_path "PATH", Formula["python"].opt_bin
ENV.prepend_path "PATH", Formula["python"].opt_libexec/"bin"
```
we should really be putting those paths in front of `ORIGINAL_PATHS` when we do the `which`. This might be the source of inconsistency here.
In the meantime, my quick and dirty workaround is setting `python_binary` to `"python2.7"`.
---
CC @DomT4 @ilovezfs