前言
目前 Python 同時(shí)更新與維護(hù) Python2 和 Python3航夺,選擇 Python2 還是選擇 Python3,取決于當(dāng)前要使用的庫(kù)、框架支持哪個(gè)版本,所以經(jīng)常會(huì)遇到切換版本的情況辽狈。那么 macOS 下應(yīng)該怎樣有效的更改呢?很多小伙伴一定會(huì)想到修改環(huán)境變量呛牲,指定 Python 的默認(rèn)路徑刮萌,這樣當(dāng)然可以,然而不夠優(yōu)雅娘扩。那么怎樣的方法才算優(yōu)雅呢着茸?當(dāng)然是一條命令了僧凤。這里通過(guò) brew 安裝 pyenv,再用 pyenv 安裝管理 Python元扔。
macOS 使用 pyenv 安裝和管理多個(gè) Python 版本
更新歷史
2018年12月16日 - 初稿
閱讀原文 - https://wsgzao.github.io/post/macos-pyenv/
擴(kuò)展閱讀
pyenv - https://github.com/pyenv/pyenv
pyenv簡(jiǎn)介
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
# 安裝管理多個(gè) 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 常用的命令說(shuō)明
使用方式: pyenv <命令> [<參數(shù)>]
命令:
commands 查看所有命令
local 設(shè)置或顯示本地的Python版本
global 設(shè)置或顯示全局Python版本
shell 設(shè)置或顯示shell指定的Python版本
install 安裝指定Python版本
uninstall 卸載指定Python版本)
version 顯示當(dāng)前的Python版本及其本地路徑
versions 查看所有已經(jīng)安裝的版本
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之前只能暫時(shí)降級(jí)Python
# 使用pyenv切換python版本
brew install pyenv
pyenv install 3.6.7
pyenv shell 3.6.7
# 創(chuàng)建PySpider虛擬環(huán)境
mkdir pyspider
cd pyspider
python -m venv venv
source venv/bin/active
pip install pyspider
# 安裝期間出現(xiàn) SSL 或 pycurl 錯(cuò)誤
__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
# 重新安裝并運(yùn)行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