macOS 使用 pyenv 安装和管理多个 Python 版本
前言
目前 Python 同时更新与维护 Python2 和 Python3,选择 Python2 还是选择 Python3,取决于当前要使用的库、框架支持哪个版本,所以经常会遇到切换版本的情况。那么 macOS 下应该怎样有效的更改呢?很多小伙伴一定会想到修改环境变量,指定 Python 的默认路径,这样当然可以,然而不够优雅。那么怎样的方法才算优雅呢?当然是一条命令了。这里通过 brew 安装 pyenv,再用 pyenv 安装管理 Python。
macOS 使用 pyenv 安装和管理多个 Python 版本
更新历史
2018年12月16日 - 初稿
阅读原文 - https://wsgzao.github.io/post/macos-pyenv/
扩展阅读
pyenv - https://github.com/pyenv/pyenv
pyenv简介
pyenv lets you easily switch between multiple versions of Python. It's simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well.
This project was forked from rbenv and ruby-build, and modified for Python.
pyenv does...
- Let you change the global Python version on a per-user basis.
- Provide support for per-project Python versions.
- Allow you to override the Python version with an environment variable.
- Search commands from multiple versions of Python at a time.
This may be helpful to test across Python versions with tox.
In contrast with pythonbrew and pythonz, pyenv does not...
- Depend on Python itself. pyenv was made from pure shell scripts. There is no bootstrap problem of Python.
-
Need to be loaded into your shell. Instead, pyenv's shim approach works by adding a directory to your
$PATH
. - Manage virtualenv. Of course, you can create virtualenv yourself, or pyenv-virtualenv to automate the process.
安装pyenv
Homebrew 是 macOS 下非常高效的命令行软件包管理器,mac 必安装工具之一
# 安装homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew -v
Homebrew 1.8.5
Homebrew/homebrew-core (git revision 497ca; last commit 2018-12-16)
Homebrew/homebrew-cask (git revision 8be7c; last commit 2018-12-17)
# 安装pyenv
brew update
brew install pyenv
pyenv -v
pyenv 1.2.8
# 安装管理多个 Python
pyenv install --list
Available versions:
2.1.3
2.2.3
2.3.7
2.4
2.4.1
2.4.2
2.4.3
2.4.4
2.4.5
2.4.6
2.5
2.5.1
2.5.2
2.5.3
2.5.4
2.5.5
2.5.6
2.6.6
2.6.7
2.6.8
2.6.9
2.7-dev
2.7
2.7.1
2.7.2
2.7.3
2.7.4
2.7.5
2.7.6
2.7.7
2.7.8
2.7.9
2.7.10
2.7.11
2.7.12
2.7.13
2.7.14
2.7.15
3.0.1
3.1
3.1.1
3.1.2
3.1.3
3.1.4
3.1.5
3.2
3.2.1
3.2.2
3.2.3
3.2.4
3.2.5
3.2.6
3.3.0
3.3.1
3.3.2
3.3.3
3.3.4
3.3.5
3.3.6
3.3.7
3.4.0
3.4-dev
3.4.1
3.4.2
3.4.3
3.4.4
3.4.5
3.4.6
3.4.7
3.4.8
3.4.9
3.5.0
3.5-dev
3.5.1
3.5.2
3.5.3
3.5.4
3.5.5
3.5.6
3.6.0
3.6-dev
3.6.1
3.6.2
3.6.3
3.6.4
3.6.5
3.6.6
3.6.7
3.7.0
3.7-dev
3.7.1
3.8-dev
activepython-2.7.14
activepython-3.5.4
activepython-3.6.0
anaconda-1.4.0
anaconda-1.5.0
anaconda-1.5.1
anaconda-1.6.0
anaconda-1.6.1
anaconda-1.7.0
anaconda-1.8.0
anaconda-1.9.0
anaconda-1.9.1
anaconda-1.9.2
anaconda-2.0.0
anaconda-2.0.1
anaconda-2.1.0
anaconda-2.2.0
anaconda-2.3.0
anaconda-2.4.0
anaconda-4.0.0
anaconda2-2.4.0
anaconda2-2.4.1
anaconda2-2.5.0
anaconda2-4.0.0
anaconda2-4.1.0
anaconda2-4.1.1
anaconda2-4.2.0
anaconda2-4.3.0
anaconda2-4.3.1
anaconda2-4.4.0
anaconda2-5.0.0
anaconda2-5.0.1
anaconda2-5.1.0
anaconda2-5.2.0
anaconda2-5.3.0
anaconda3-2.0.0
anaconda3-2.0.1
anaconda3-2.1.0
anaconda3-2.2.0
anaconda3-2.3.0
anaconda3-2.4.0
anaconda3-2.4.1
anaconda3-2.5.0
anaconda3-4.0.0
anaconda3-4.1.0
anaconda3-4.1.1
anaconda3-4.2.0
anaconda3-4.3.0
anaconda3-4.3.1
anaconda3-4.4.0
anaconda3-5.0.0
anaconda3-5.0.1
anaconda3-5.1.0
anaconda3-5.2.0
anaconda3-5.3.0
ironpython-dev
ironpython-2.7.4
ironpython-2.7.5
ironpython-2.7.6.3
ironpython-2.7.7
jython-dev
jython-2.5.0
jython-2.5-dev
jython-2.5.1
jython-2.5.2
jython-2.5.3
jython-2.5.4-rc1
jython-2.7.0
jython-2.7.1
micropython-dev
micropython-1.9.3
micropython-1.9.4
miniconda-latest
miniconda-2.2.2
miniconda-3.0.0
miniconda-3.0.4
miniconda-3.0.5
miniconda-3.3.0
miniconda-3.4.2
miniconda-3.7.0
miniconda-3.8.3
miniconda-3.9.1
miniconda-3.10.1
miniconda-3.16.0
miniconda-3.18.3
miniconda2-latest
miniconda2-3.18.3
miniconda2-3.19.0
miniconda2-4.0.5
miniconda2-4.1.11
miniconda2-4.3.14
miniconda2-4.3.21
miniconda2-4.3.27
miniconda2-4.3.30
miniconda3-latest
miniconda3-2.2.2
miniconda3-3.0.0
miniconda3-3.0.4
miniconda3-3.0.5
miniconda3-3.3.0
miniconda3-3.4.2
miniconda3-3.7.0
miniconda3-3.8.3
miniconda3-3.9.1
miniconda3-3.10.1
miniconda3-3.16.0
miniconda3-3.18.3
miniconda3-3.19.0
miniconda3-4.0.5
miniconda3-4.1.11
miniconda3-4.2.12
miniconda3-4.3.11
miniconda3-4.3.14
miniconda3-4.3.21
miniconda3-4.3.27
miniconda3-4.3.30
pypy-c-jit-latest
pypy-c-nojit-latest
pypy-dev
pypy-stm-2.3
pypy-stm-2.5.1
pypy-1.5-src
pypy-1.5
pypy-1.6
pypy-1.7
pypy-1.8
pypy-1.9
pypy-2.0-src
pypy-2.0
pypy-2.0.1-src
pypy-2.0.1
pypy-2.0.2-src
pypy-2.0.2
pypy-2.1-src
pypy-2.1
pypy-2.2-src
pypy-2.2
pypy-2.2.1-src
pypy-2.2.1
pypy-2.3-src
pypy-2.3
pypy-2.3.1-src
pypy-2.3.1
pypy-2.4.0-src
pypy-2.4.0
pypy-2.5.0-src
pypy-2.5.0
pypy-2.5.1-src
pypy-2.5.1
pypy-2.6.0-src
pypy-2.6.0
pypy-2.6.1-src
pypy-2.6.1
pypy-4.0.0-src
pypy-4.0.0
pypy-4.0.1-src
pypy-4.0.1
pypy-5.0.0-src
pypy-5.0.0
pypy-5.0.1-src
pypy-5.0.1
pypy-5.1-src
pypy-5.1
pypy-5.1.1-src
pypy-5.1.1
pypy-5.3-src
pypy-5.3
pypy-5.3.1-src
pypy-5.3.1
pypy-5.4-src
pypy-5.4
pypy-5.4.1-src
pypy-5.4.1
pypy-5.6.0-src
pypy-5.6.0
pypy-5.7.0-src
pypy-5.7.0
pypy-5.7.1-src
pypy-5.7.1
pypy2-5.3-src
pypy2-5.3
pypy2-5.3.1-src
pypy2-5.3.1
pypy2-5.4-src
pypy2-5.4
pypy2-5.4.1-src
pypy2-5.4.1
pypy2-5.6.0-src
pypy2-5.6.0
pypy2-5.7.0-src
pypy2-5.7.0
pypy2-5.7.1-src
pypy2-5.7.1
pypy2.7-5.8.0-src
pypy2.7-5.8.0
pypy2.7-5.9.0-src
pypy2.7-5.9.0
pypy2.7-5.10.0-src
pypy2.7-5.10.0
pypy2.7-6.0.0-src
pypy2.7-6.0.0
pypy3-dev
pypy3-2.3.1-src
pypy3-2.3.1
pypy3-2.4.0-src
pypy3-2.4.0
pypy3.3-5.2-alpha1-src
pypy3.3-5.2-alpha1
pypy3.3-5.5-alpha-src
pypy3.3-5.5-alpha
pypy3.5-c-jit-latest
pypy3.5-5.7-beta-src
pypy3.5-5.7-beta
pypy3.5-5.7.1-beta-src
pypy3.5-5.7.1-beta
pypy3.5-5.8.0-src
pypy3.5-5.8.0
pypy3.5-5.9.0-src
pypy3.5-5.9.0
pypy3.5-5.10.0-src
pypy3.5-5.10.0
pypy3.5-5.10.1-src
pypy3.5-5.10.1
pypy3.5-6.0.0-src
pypy3.5-6.0.0
pyston-0.5.1
pyston-0.6.0
pyston-0.6.1
stackless-dev
stackless-2.7-dev
stackless-2.7.2
stackless-2.7.3
stackless-2.7.4
stackless-2.7.5
stackless-2.7.6
stackless-2.7.7
stackless-2.7.8
stackless-2.7.9
stackless-2.7.10
stackless-2.7.11
stackless-2.7.12
stackless-2.7.14
stackless-3.2.2
stackless-3.2.5
stackless-3.3.5
stackless-3.3.7
stackless-3.4-dev
stackless-3.4.1
stackless-3.4.2
stackless-3.4.7
stackless-3.5.4
# 选择Python所需版本
pyenv install 2.7.15
pyenv install 3.6.7
# 初始化
pyenv init
# Load pyenv automatically by appending
# the following to ~/.zshrc:
eval "$(pyenv init -)"
# 重启zsh
exec "$SHELL"
# 切换版本
pyenv global 2.7.15
pyenv shell 3.6.7
# 查看版本
pyenv versions
system
2.7.15
* 3.6.7 (set by PYENV_VERSION environment variable)
# pyenv 常用的命令说明
使用方式: pyenv <命令> [<参数>]
命令:
commands 查看所有命令
local 设置或显示本地的Python版本
global 设置或显示全局Python版本
shell 设置或显示shell指定的Python版本
install 安装指定Python版本
uninstall 卸载指定Python版本)
version 显示当前的Python版本及其本地路径
versions 查看所有已经安装的版本
which 显示安装路径
pyenv
pyenv 1.2.8
Usage: pyenv <command> [<args>]
Some useful pyenv commands are:
commands List all available pyenv commands
local Set or show the local application-specific Python version
global Set or show the global Python version
shell Set or show the shell-specific Python version
install Install a Python version using python-build
uninstall Uninstall a specific Python version
rehash Rehash pyenv shims (run this after installing executables)
version Show the current Python version and its origin
versions List all Python versions available to pyenv
which Display the full path to an executable
whence List all Python versions that contain the given executable
See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme
Mac 中安装 PySpider
在PySpider支持Python3.7之前只能暂时降级Python
# 使用pyenv切换python版本
brew install pyenv
pyenv install 3.6.7
pyenv shell 3.6.7
# 创建PySpider虚拟环境
mkdir pyspider
cd pyspider
python -m venv venv
source venv/bin/active
pip install pyspider
# 安装期间出现 SSL 或 pycurl 错误
__main__.ConfigurationError: Curl is configured to use SSL, but we have not been able to determine which SSL backend it is using. Please see PycURL documentation for how to specify the SSL backend manually.
# 解决方法
pip uninstall pycurl
export PYCURL_SSL_LIBRARY=openssl
export LDFLAGS=-L/usr/local/opt/openssl/lib;export CPPFLAGS=-I/usr/local/opt/openssl/include;pip install pycurl --compile --no-cache-dir
# 重新安装并运行pyspider
pip install pyspider
pyspider
[W 181217 15:23:35 run:413] phantomjs not found, continue running without it.
[I 181217 15:23:37 result_worker:49] result_worker starting...
[I 181217 15:23:37 processor:211] processor starting...
[I 181217 15:23:37 tornado_fetcher:638] fetcher starting...
[I 181217 15:23:37 scheduler:647] scheduler starting...
[I 181217 15:23:37 scheduler:782] scheduler.xmlrpc listening on 127.0.0.1:23333
[I 181217 15:23:37 scheduler:586] in 5m: new:0,success:0,retry:0,failed:0
[I 181217 15:23:37 app:76] webui running on 0.0.0.0:5000