Mac:安装Python3并配置环境变量(本地多个Python版本选择配置)

简介: Mac:安装Python3并配置环境变量(本地多个Python版本选择配置)

前期检查


mac是否自带python


看到网上说Mac是自带内置Python(python2)的,但是自macOS 12.3版本后,将不再会自带,并建议大家使用python3


查看当前电脑自带Python版本:

python --version


⚠️:如果是电脑本身有python3,但是又没有配置好环境变量,可以通过下面脚本检查

python3 --version


python都安装在哪里

# 如果是python2
where python
# 如果是python3
where python3


如下,可以看到我的python3在多个地方


ba31d8f3dca544ce8716806efbb4de2e.png


当前使用的是哪个python

which python

如下是我配置好的展示


ca2efed701004c6386ab2fff21109668.png

如果想要安装使用Python3,那么可以继续下面步骤

安装python3

安装有两种方式

1.通过brew安装Python3


首先说明,我尝试使用了该方法安装,但是配置环境变量时,找不到对应的配置,后来选择了第2种方式,这里也建议直接选择第2种方式

brew install python3


2.通过官网下载安装Python3

python官网

(1)官网—>Downloads—>macOS点击



image.png

(2)选择合适的版本下载

(建议选择稳定版)


image.png


(3)安装

下载完成之后,点击安装。按照提示点击继续,一路火花🔥带闪电⚡️,直到提示安装成功。

配置环境变量

验证

下载安装完成


通过官网下载安装的方式,可以在路径

cd /Library/Frameworks/Python.framework/Versions/

看到安装的python3版本,如下,我安装的python3.11版本

image.png

如果是通过brew方式安装的,上面的路径是找不到的,导致我不知道环境变量怎么配置

配置环境变量


在~/.bash_profile中配置环境变量

sudo vim ~/.bash_profile


在最后一行添加python的环境变量,我的如下:

记得根据自己的实际路径替换

export PATH="/Library/Frameworks/Python.framework/Versions/3.11/bin:${PATH}"
alias python="/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11"


保存配置

source ~/.bash_profile

至此,理论上是没有问题了

如下是我已经配置好的


52d43c65c4084f0ca0f2724a114bdaaf.png

再次查看当前使用的python

which python
which python3
python


可以发现使用上面的命令,输出路径都是环境变量配置好的

00a12246d26441509015652c3c89d409.png

可以去使用python3了

目录
相关文章
|
8天前
|
Linux iOS开发 MacOS
如何查看你的Python版本?
在命令行中查看Python版本很简单。在Windows上按Win+R,输入powershell;在macOS上通过Finder→Applications→Utilities→Terminal;在Linux上打开终端。然后输入`python --version`或`python -V`。输出显示如"Python 3.8.3"。使用`python -VV`可获取更多详细信息。在Python脚本中,可通过`sys.version`或`platform.python_version()`检查版本。确保使用Python 3,因为Python 2自2020年起已停止更新和支持。
|
6天前
|
开发者 Python
【Python】已解决:(Python3中pip无法安装urllib报错问题) ERROR: Could not find a version that satisfies the requireme
【Python】已解决:(Python3中pip无法安装urllib报错问题) ERROR: Could not find a version that satisfies the requireme
13 0
【Python】已解决:(Python3中pip无法安装urllib报错问题) ERROR: Could not find a version that satisfies the requireme
|
6天前
|
iOS开发 MacOS Python
【Python】已解决:(Pycharm切换Python版本后报错)No Python at “C:\Program Files\Python39\python.exe”
【Python】已解决:(Pycharm切换Python版本后报错)No Python at “C:\Program Files\Python39\python.exe”
11 0
【Python】已解决:(Pycharm切换Python版本后报错)No Python at “C:\Program Files\Python39\python.exe”
|
11天前
|
达摩院 语音技术 异构计算
语音识别-免费开源的语音转文本软件Whisper的本地搭建详细教程,python版本是3.805,ffmpeg是专门处理音视频的,ffmpeg的下载链接,现在要求安装python和ffmpeg
语音识别-免费开源的语音转文本软件Whisper的本地搭建详细教程,python版本是3.805,ffmpeg是专门处理音视频的,ffmpeg的下载链接,现在要求安装python和ffmpeg
|
21天前
|
Python
如何查询Python包的所有历史版本
如何查询Python包的所有历史版本
17 5
|
26天前
|
Python
Python语言提供了多种输出格式化的方法,这些方法随着时间的推移和版本的更新而发展
【6月更文挑战第19天】Python格式化方法包括过时的`%`操作符,`str.format()`,推荐的f-string(Python 3.6+)和Template strings。f-string提供最佳的可读性和性能,`str.format()`是通用的,而`%`不推荐使用。模板字符串用于特定场景。对于旧版Python,使用`str.format()`或`%`。
24 4
|
5天前
|
JavaScript 前端开发 Java
python结构化模式匹配switch-case,Python 3.10中引入,Python的模式匹配(pattern matching)语法
python结构化模式匹配switch-case,Python 3.10中引入,Python的模式匹配(pattern matching)语法
|
1月前
|
存储 SQL Rust
[译] Python各版本的主要更改
[译] Python各版本的主要更改
|
24天前
|
存储 Java API
Mac安装jadx并配置环境
Mac安装jadx并配置环境
29 0
|
1月前
|
Java Maven
【已解决】MAC安装maven,The JAVA_HOME environment variable is not defined correctly, this environment
【已解决】MAC安装maven,The JAVA_HOME environment variable is not defined correctly, this environment
54 0