50

After switching to python 3.4.3 from 2.7.9 (which was quite simple), I often wish to test some of my scripts with python 2.7.9 before sharing them with colleagues. I am using a OSX yosemite platform with everything compiled from homebrew.

The situation was quite ugly (setting PATHes and PYTHONPATH at each step) - until I discovered pyenv which does this very easily and is easily installed using homebrew. So far, so good.

However, now that I am using this version of python, it does not necessarily play well with that of homebrew. Moreover, I found that I could switch back to the system's python, and more generally that pyenv could access that:

$ pyenv versions
  system
  2.7.9
* 3.4.3 (set by /usr/local/var/pyenv/version)

but how could I also add entries for the pythons compiled by homebrew?

meduz
  • 3,903
  • 1
  • 28
  • 40

7 Answers7

61

You can install pyenv in your home directory (as described in pyenv's installation guide), and then create a symlink at ~/.pyenv/versions to $(brew --cellar)/python:

ln -s $(brew --cellar python)/* ~/.pyenv/versions/

The way Homebrew works nowadays, this will pick up both 2.x and 3.x.

Andrew Aylett
  • 39,182
  • 5
  • 68
  • 95
mipadi
  • 398,885
  • 90
  • 523
  • 479
  • This won't work if the versions folder already exists, and won't include python3. Also, I'm pretty sure you meant: ln -s $(brew --cellar)/python ~/.pyenv/versions – misnomer Oct 08 '15 at 17:44
  • 2
    Actually, you need to run `ln -s $(brew --cellar python)/* ~/.pyenv/versions`. Otherwise the python version added will be called `python` instead of `2.7.0` – Hanxue Dec 23 '15 at 08:15
  • Since I was trying to list my homebrew installed python3 environments in pyenv, this following worked for me: `ln -s $(brew --cellar python3)/* ~/.pyenv/versions` did the trick for me – JacobWuzHere Nov 02 '16 at 16:00
  • 2
    For Homebrew's Python 2, the formula is now called python@2. If `brew list -1 | grep python@2` prints `python@2` instead of nothing, then you have Homebrew's version of Python 2 installed. If you want it to be available in pyenv, then you'll need to slightly modify the command in the answer to: `ln -s $(brew --cellar python@2)/* ~/.pyenv/versions/` – WalterGR May 03 '18 at 23:49
  • 7
    Is there a way to make homebrew use pyenv installed Python? I tried `ln -s ~/.pyenv/versions/3.7.3 $HOME/homebrew/Cellar/python`, but homebrew still tries to install its own Python. – laike9m Jul 21 '19 at 05:16
  • 6
    To include all Python versions installed by homebrew, I linked to all `python*` directories in the homebrew cellar using `ln -s $(brew --cellar)/python*/* ~/.pyenv/versions/` (note `/python*` used to match any cellar directory starting with "python" and trailing `/*` to link subdirectories). This avoids the need to special-case specific versions (see comments by @ JacobWuzHere, @WalterGR, and @laike9m) – Tony S Yu Jan 02 '20 at 19:19
9

[2022] The python3 versions in homebrew is now in the format of python@3.x so, updated shell

#!/bin/bash

pyenv-brew-relink() {
    rm -f "$HOME/.pyenv/versions/*-brew"
    for i in $(brew --cellar)/python* ; do
      for p in $i/*; do
        echo $p
        ln -s -f $p $HOME/.pyenv/versions/${p##/*/}-brew
      done  
    done
    pyenv rehash
}

pyenv-brew-relink

user5354671
  • 311
  • 4
  • 4
  • Shouldn't it be `for i in $(brew --cellar)/python@*; do` (added *@*), so that other formulas are not linked which also start with `python` (e.g. `python-tk@3.11` or `python-certifi`)? – coreuter Aug 14 '23 at 13:10
7

A handy function to relink versions:

pyenv-brew-relink() {
  rm -f "$HOME/.pyenv/versions/*-brew"

  for i in $(brew --cellar python)/*; do
    ln -s --force $i $HOME/.pyenv/versions/${i##/*/}-brew;
  done

  for i in $(brew --cellar python@2)/*; do
    ln -s --force $i $HOME/.pyenv/versions/${i##/*/}-brew;
  done
}
decay_of_mind
  • 91
  • 1
  • 3
  • 1
    Ah, the magic of `${i##/*/}`. In case someone is wondering what is happening there, see [this](https://tldp.org/LDP/abs/html/string-manipulation.html) – Fanchen Bao Jan 12 '21 at 22:15
4

Pulling all the bits of the previous answers together for one actually working ring to bind them:

pyenv-brew-relink() {
    rm -f "$HOME/.pyenv/versions/*-brew"
    for i in $(brew --cellar)/python* ; do
        ln -s -f "$p" "$HOME/.pyenv/versions/${i##/*/}-brew"
    done
    pyenv rehash
}
tesch1
  • 2,756
  • 1
  • 14
  • 11
3

Well if you want the pyenv pythons and homebrew pythons to live together you need to make the name of the homebrew pythons something other than the version. Otherwise they will clash with the directory names that pyenv uses. For example, if you want to install pyenv python 2.7.11 and homebrew python 2.7.11 you could do something like this.

for i in `ls $(brew --cellar python)/`; do 
  ln -s $(brew --cellar python)/$i $HOME/.pyenv/versions/$i-brew; 
done

for i in `ls $(brew --cellar python3)/`; do 
  ln -s $(brew --cellar python)/$i $HOME/.pyenv/versions/$i-brew; 
done

Essentially this will create a directory in $HOME/.pyenv/versions appended with '-brew' so that it won't clash with the pyenv pythons.

johnrizzo1
  • 31
  • 3
3

Just to add to @johnizzo1's answer, python2 is now python@2, so you should change the python3 for loop to something like:

for i in `ls $(brew --cellar python)/`; do 
  ln -s $(brew --cellar python)/$i $HOME/.pyenv/versions/$i-brew; 
done

for i in `ls $(brew --cellar python@2)/`; do 
  ln -s $(brew --cellar python@2)/$i $HOME/.pyenv/versions/$i-brew; 
done
xuru
  • 31
  • 1
2

pyenv will use system as default version when version is not specified.

When you install python@3 by homebrew and pyenv's version is specified to system, python points to python 2.x in the system and python3 points to python@3 which installed by homebrew.

So usually we don't need to manually add version to pyenv.


In 2020, after `ln -s /outside/python/x.x.x ~/.pyenv/versions/x.x.x`, you need `pyenv rehash` to rehash shims.

Example: add macos system python 2.7 to pyenv

ln -s /System/Library/Frameworks/Python.framework/Versions/2.7 ~/.pyenv/versions/2.7

pyenv rehash
Dong
  • 33
  • 1
  • 7
  • While this is technically true, you will still need to add Python versions to `pyenv` when you need multiple different versions of `python3` for your projects, like `3.9` and `3.10`. – justarandomguy Dec 22 '21 at 07:48