pyenv 是管理 Python 版本的重要工具,在开发环境中是必备的软件。

使用 brew

在 Mac 上使用 brew 可以很方便的安装 pyenv

安装

1
$ brew install pyenv

接下来需要修改环境变量
vim ~/.bash_profile

1
2
3
4
5
6
7
8
export PYENV_ROOT="${HOME}/.pyenv"
if [ -d "${PYENV_ROOT}" ]; then
  export PATH="${PYENV_ROOT}/bin:${PATH}"
  eval "$(pyenv init -)"
fi

CFLAGS="-I$(brew --prefix openssl)/include" \
LDFLAGS="-L$(brew --prefix openssl)/lib" \

使配置生效 source ~/.bash_profile
然后还需要一些依赖

1
2
$ xcode-select --install
$ brew install readline xz zlib

这样就可以正常使用 pyenv 了

升级

1
$ brew upgrade pyenv

卸载

1
$ brew uninstall pyenv

然后删除之前填在 ~/.bash_profile 文件里添加的内容

直接安装

但是如果你电脑上没有安装 brew,那再去安装它就麻烦多了,如果着急用,我们可以直接安装。

下载源代码

1
$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv

配置环境变量

1
2
3
4
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
$ source ~/.bash_profile

看,就是这么简单,此时运行 pyenv,机器已经可以识别。

升级

1
2
$ cd ~/.pyenv
$ git pull

卸载

1
$ rm -rf ~/.pyenv

随后删除 ~/.bash_profile 中添加的内容即可

03-16 17:41