Mac电脑Python 路径和版本问题排查指南

最近安装一个软件,显示python版本,找了一下电脑的python版本,好几个,懵了。

不知道用哪个,干脆把所有环境都删除了,重新安装了一个,

把排除python的路径的过程记录下来了。

  1. 确认 Python 是否已安装

首先,检查系统中是否已安装 Python。可以使用以下命令:

复制代码
python --versionpython3 --version

如果系统返回版本号,说明 Python 已安装。如果返回 command not found,则需要安装 Python。

  1. 安装 Python

如果 Python 未安装,可以使用 Homebrew 安装(适用于 macOS):

复制代码
brew install python@3.10  # 或其他版本
  1. 查找 Python 的安装路径

使用 find 命令查找 Python 的安装路径:

复制代码
find /usr -name "python3*"

或者,使用以下命令查找 Homebrew 安装的 Python 版本:​​​​​​​

复制代码
ls /usr/local/Cellar/python@3.10/*/bin/python3# 或者对于 Apple Siliconls /opt/homebrew/Cellar/python@3.10/*/bin/python3
  1. 创建符号链接

如果您希望通过 python 或 python3 命令访问特定版本的 Python,您需要创建符号链接。假设您找到的 Python 3.10 的路径是 /opt/homebrew/Cellar/python@3.10/3.10.16/bin/python3.10,可以使用以下命令创建符号链接:

复制代码
sudo ln -sf /opt/homebrew/Cellar/python@3.10/3.10.16/bin/python3.10 /opt/homebrew/bin/python3
  1. 确保路径在环境变量中

确保 Python 的安装路径在您的 PATH 环境变量中。可以通过以下命令检查:

复制代码
echo $PATH

如果 /opt/homebrew/bin 不在输出中,您可以在 ~/.zshrc 文件中添加它:

复制代码
vim ~/.zshrc

在文件末尾添加:

复制代码
export PATH="/opt/homebrew/bin:$PATH"

保存并退出,然后运行:

复制代码
source ~/.zshrc
  1. 设置别名(可选)

如果希望在使用 python 命令时指向 Python 3,可以在 ~/.zshrc 文件中设置别名:

复制代码
alias python=python3

保存并退出,然后运行:

复制代码
source ~/.zshrc
  1. 验证 Python 版本

最后,验证 Python 版本是否正确:​​​​​​​

复制代码
python --versionpython3 --version
相关推荐
SelectDB4 小时前
Apache Doris Python UDF:让 SQL 直接调用 Python 生态,支撑 Agent 时代复杂业务逻辑
大数据·数据库·python
Mintimate5 小时前
WorkBuddy 上手: 让脚本项目 Homebrew CN 变成会排障的 Agent
macos·边缘计算·agent
荣码11 小时前
GraphRAG:普通RAG只能回答"点"的问题,我踩了4个坑才搞懂
java·python
金銀銅鐵1 天前
[Python] 基于欧几里得算法,实现分数约分计算器
python·数学
Lyn_Li1 天前
Kaggle Top 5 | 198只股票、200条数据的金融预测——BattleFin高分方案从零复现
python·kaggle·比赛复盘·金融预测
小九九的爸爸1 天前
前端想要入门Agent开发,要具备哪些Python基础?
python·agent·ai编程
阿耶同学1 天前
手把手教你用 LangGraph 搭建三层嵌套 Agent 架构
python·程序员
fthux1 天前
如果你用 Mac,那你可能需要 Noti Shift
macos·开源·github
花酒锄作田2 天前
Pydantic校验配置文件
python
hboot2 天前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络