mac以前会自带2.7版本的python,现在会在安装命令行工具后默认安装3.9.6版本的python。 这里讨论的情况是mac本体已经带了3.9.6版本的python。命令行检查为:
mcd@maidanglaodeMacBook-Air ~ % where python3
/usr/bin/python3
Homebrew官网为:https://brew.sh/ 可以使用官网里面的命令进行安装(需要翻墙环境),也可以使用官网里面推荐的pkg安装
这基本上是pkg安装后的问题,原因是环境变量没有设置好。
-
编辑/etc/profile文件
sudo vim /etc/profile
按
i
进入编辑模式,在文件最后写入以下内容export PATH="/opt/homebrew/bin:$PATH"
按
esc
退出编辑模式,输入:wq!
保存文件并退出vim 执行source /etc/profile
安装python
brew install python3
指定版本
brew install [email protected]
brew安装完python的时候会有一系列文本:
Python is installed as
/opt/homebrew/bin/python3.10
Unversioned and major-versioned symlinks `python`, `python3`, `python-config`, `python3-config`, `pip`, `pip3`, etc. pointing to
`python3.10`, `python3.10-config`, `pip3.10` etc., respectively, are installed into
/opt/homebrew/opt/[email protected]/libexec/bin
You can install Python packages with
pip3.10 install <package>
They will install into the site-package directory
/opt/homebrew/lib/python3.10/site-packages
tkinter is no longer included with this formula, but it is available separately:
brew install [email protected]
If you do not need a specific version of Python, and always want Homebrew's `python3` in your PATH:
brew install python3
See: https://docs.brew.sh/Homebrew-and-Python
这里说明了python3.10和pip3.10安装在了哪里,如果没有保存文本,可以通过命令where python3.10
查询
-
编辑
~/.zshrc
文件vim ~/.zshrc
-
按
i
键进入编辑模式,在文件最后添加内容export PATH="/opt/homebrew/bin:$PATH" alias python3='/opt/homebrew/bin/python3.10' alias pip3='/opt/homebrew/bin/pip3.10'
按
esc
退出编辑模式,输入:wq!
保存文件并退出vim 执行source ~/.zshrc