mac是自带python2.7版本的,
官网下载了python3.9社区版,
在未更改默认路径前,终端默认输入python启动的是2.7版本而非3.9版本。
以下是我在修改版本的过程中遇到的一些问题,若无这些问题可以直接跳至文章末尾
查找python具体安装位置一(无需打开python查找):
- 打开终端
- 输入“which python” / “which python3”
- 得到对应版本python安装位置
kkeria@huangjialideMBP ~ % which python3
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
查找python具体安装位置二(打开python查找):
- 打开终端
- 输入“python”
- 输入“import sys"
- 输入“print(sys.path)”
- 打印得到一个列表,存放着python搜索模块的路径集
kkeria@huangdeMBP ~ % python
WARNING: Python 2.7 is not recommended.
This version is included in macOS for compatibility with legacy software.
Future versions of macOS will not include Python 2.7.
Instead, it is recommended that you transition to using 'python3' from within Terminal.
Python 2.7.16 (default, Dec 21 2020, 23:00:36)
[GCC Apple LLVM 12.0.0 (clang-1200.0.30.4) [+internal-os, ptrauth-isa=sign+stri on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/Users/kkeria/Library/Python/2.7/lib/python/site-packages',
'/Library/Python/2.7/site-packages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
'/Library/Python/2.7/site-packages/virtualenv-20.4.7-py2.7.egg',
'/Library/Python/2.7/site-packages/pathlib2-2.3.5-py2.7.egg',
'/Library/Python/2.7/site-packages/pip-21.1.2-py2.7.egg']
这里还未更改终端python的默认版本,所以显示了warning。
这种情况下,如果需要查找python3的安装位置,步骤与以上一致,只需将第一步的“python”改成“python3”即可
kkeria@huangjialideMBP ~ % python3
Python 3.9.5 (v3.9.5:0a7dcbdb13, May 3 2021, 13:05:53)
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', '/Library/Frameworks/Python.framework/Versions/3.9/lib/python39.zip',
'/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9',
'/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages']
从以上操作可以得到:
python2.7所在位置:(系统自带所以在System的文件夹中)
file:///System/Library/Frameworks/Python.framework/Versions/2.7
python2.7第三方库所在位置:
file:///System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
python3.9所在位置:
file:///Library/Frameworks/Python.framework/Versions/3.9
python3.9第三方库所在位置:
file:///Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9
注意:两个版本并不处在同一个文件夹中
关于mac查找指定路径下文件的方法:
- 打开访达
- 快捷键 command+shift+G 打开前往文件夹的窗口
- 粘贴指定路径
- 前往指定文件位置
关于mac中复制文件路径的方法:
- 打开访达
- 找到指定文件/文件夹
- 将文件/文件夹拖至浏览器的地址栏
- 地址栏显示文件路径,复制即可用
关于修改mac终端上的默认python版本(zsh):(2.7->3.9)
*以下操作只适合shell为zsh的MAC(终端标题上有显示)
- 打开终端
- 输入"which python3",复制得到的python3.9安装位置
- 输入"open ~/.zshrc"
- 如果成功打开配置文件(下图名为“.zshrc“的文件弹窗),则进入下一步;如果没有打开文件,则输入一下代码新建文件
kkeria@huangdeMBP ~ % cd ~
kkeria@huangdeMBP ~ % touch .zshrc
- 在配置文件中输入“alias python="(粘贴刚刚复制的python安装位置)"”,左上角文件保存或command+S保存文件,关闭文件。
- 输入“source ~/.zshrc",应用配置文件
- 输入“python”,就会发现python默认版本已经改变。(关闭终端,重新打开,输入python确认确实更改了)
kkeria@huangdeMBP ~ % which python3
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
kkeria@huangdeMBP ~ % open ~/.zshrc
kkeria@huangdeMBP ~ % source ~/.zshrc
kkeria@huangdeMBP ~ % python
Python 3.9.5 (v3.9.5:0a7dcbdb13, May 3 2021, 13:05:53)
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>