前言

目前 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.

Terminal output example

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 必安装工具之一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 安装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

# 选择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)

# 升级切换python版本
pyenv install 3.10
pyenv global 3.10.10

pyenv install 3.11
pyenv global 3.11.2

# 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 使用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

文章目录
  1. 1. 前言
  2. 2. 更新历史
  3. 3. pyenv简介
  4. 4. 安装pyenv
  5. 5. Mac 中安装 PySpider