How can I make homebrew's python and pyenv live together?

雨燕双飞 提交于 2020-06-23 23:54:16

问题


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?


回答1:


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.




回答2:


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.




回答3:


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
}



回答4:


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


来源:https://stackoverflow.com/questions/30499795/how-can-i-make-homebrews-python-and-pyenv-live-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!