今天把Mac上的ffmpeg升级了一下
>>> brew upgrade ffmpeg
谁知道新的ffmpeg依赖Python3.9
一连串依赖安装下来:
现在系统存在三个版本的Python:
可以看到三个版本的路径分别为:
- Python3.8.2: /usr/bin
- Python3.9.1: /usr/local/opt/python@3.9/bin
- Python3.8.7: /usr/local/opt/python@3.8/bin
现在的Python3为Python3.9.1,而之前Python3为Python3.8.2
可以看到Python版本的变化并没有影响到pip,命令pip和pip3依旧为Python3.8的
此时如果你希望将Python3改为Python3.8,而又不卸载Python3.9,只需要改一下/usr/local/bin/python3的软连接
>>> unlink /usr/local/bin/python3
# 此时python3变为了系统自带的python3.8.2版
>>> unlink /usr/local/bin/python3-config
# 此时系统找不到python3-config
>>> ln -s /usr/local/Cellar/python@3.8/3.8.7/bin/python3 /usr/local/bin/python3
>>> ln -s /usr/local/Cellar/python@3.8/3.8.7/bin/python3-config /usr/local/bin/python3-config
参考:
https://stackoverflow.com/a/38806058
如果你之前是通过get-pip.py文件安装的pip,则最好是将其卸载,因为brew安装的python已经包含pip工具
作为Mac上管理包的神器,homebrew管理python版本更简单:
其实就是自动帮你在/usr/local/bin中创建python和pip的软连接
而/usr/local/bin的搜索优先级高于/usr/bin(在PATH环境变量中更靠前),会优先在前者中找python3和pip3
# 打印环境变量
>>> echo $PATH
/Users/microfat/Library/Python/3.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Apple/usr/bin:/Applications/Wireshark.app/Contents/MacOS
总结:
- 不要使用python3 get-pip.py安装pip,因为通过brew安装的python都自带pip
- 使用brew link {python@version}管理python版本,简单高效
- 使用docker或者venv、pyenv这些虚拟环境管理多版本
如果出现:
>>> brew link python@3.9
Linking /usr/local/Cellar/python@3.9/3.9.1_4...
Error: Could not symlink bin/pip3.9
Target /usr/local/bin/pip3.9
already exists. You may want to remove it:
rm '/usr/local/bin/pip3.9'
To force the link and overwrite all conflicting files:
brew link --overwrite python@3.9
To list all files that would be deleted:
brew link --overwrite --dry-run python@3.9
则按照提示删除就好了
关于系统自带的Python:
macOS最开始只安装有python2.7,而在安装homebrew时会被要求安装Command Line Tools (CLT) for Xcode
此时Python3.8.2就被安装在xcode目录下了
/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/bin
参考:
https://docs.brew.sh/Installationhttps://medium.com/faun/the-right-way-to-set-up-python-on-your-mac-e923ffe8cf8e
2022-10-27更新
今天homebrew终于放出了Python3.11.0版本,准备安装尝试一下,按照往常执行如下命令
>>> brew install python@3.11
>>> brew unlink python@3.10
>>> brew link python@3.11
但做完以后,发现执行python3命令时,出现的竟然是python3.9.6,即系统自带的Pyhton版本
进一步查看
>>> ll /usr/local/bin/python*
lrwxr-xr-x 1 microfat admin 43B Oct 27 11:19 /usr/local/bin/python3.11 -> ../Cellar/python@3.11/3.11.0/bin/python3.11
lrwxr-xr-x 1 microfat admin 50B Oct 27 11:19 /usr/local/bin/python3.11-config -> ../Cellar/python@3.11/3.11.0/bin/python3.11-config
lrwxr-xr-x 1 microfat admin 41B Oct 27 10:36 /usr/local/bin/python3.9 -> ../Cellar/python@3.9/3.9.15/bin/python3.9
lrwxr-xr-x 1 microfat admin 48B Oct 27 10:36 /usr/local/bin/python3.9-config -> ../Cellar/python@3.9/3.9.15/bin/python3.9-config
可以看到并没有python3的软链
重复尝试多次后发现,只有brew link python@3.10
时能够生效,其他版本的python都生成不了python3软链
找了很久也没查出问题原因,如果您知道,请您一定不吝赐教🙏
最后只能放弃brew link
改用手动添加环境变量的方式了
>>> vim .zshrc
...
export PATH="$(brew --prefix python@3.11)/libexec/bin:$PATH"
...
>>> source .zshrc
参考:https://github.com/Homebrew/homebrew-core/issues/107904#issuecomment-1213272960