我的 Python 如何在不设置 PATH 的情况下指向 Python3?

How does my Python point to Python3 without setting PATH?

我的 mac 默认带有 Python2。我使用 Homebrew (brew install python3) 安装了 Python3 并做了 某事 python 指向终端中的 python3。不幸的是,我不记得我做了什么(我一定是随便复制并粘贴了几行命令来将 python 设置为 python3)。现在我想找出我做了什么被遗忘的事情,我在我的 $PATHS、我的 .bash_profile 中到处寻找, .zhrc 等,但我发现 python 或 python3 没有任何设置 - 没有写入路径,也没有别名。

这怎么可能,我还能做些什么?

我的 Python 版本:

my-mac:~ myname$ python2

Python 2.7.16 (default, Feb 28 2021, 12:34:25) 
[GCC Apple LLVM 12.0.5 (clang-1205.0.19.59.6) [+internal-os, ptrauth-isa=deploy on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

my-mac:~ myname$ python

Python 3.9.6 (default, Jun 29 2021, 05:25:02) 
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

这是我所有的 PATH:

my-mac:~ myname$ echo "${PATH//:/$'\n'}"

/Users/myname/.rvm/gems/ruby-1.9.3-p327/bin
/Users/myname/.rvm/gems/ruby-1.9.3-p327@global/bin
/Users/myname/.rvm/rubies/ruby-1.9.3-p327/bin
/Users/myname/.rvm/bin
/usr/local/mysql/bin
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/Library/TeX/texbin
/Users/myname/Qt/5.5/clang_64/bin/

正在检查 Python 和 Python3 路径:

my-mac:~ myname$ type python
python is hashed (/usr/local/bin/python)

my-mac:~ myname$ type python3
python3 is /usr/local/bin/python3

我的.bash_profile.zshrc内容:

my-mac:~ myname$ nano .bash_profile

export PATH="/usr/local/mysql/bin:$PATH"
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

my-mac:~ myname$ nano .zshrc

PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting

brew 在 /usr/local/bin 处安装二进制文件,在您的 PATH 中 /usr/bin 之前。 (brew 更新 PATH 以在系统 /usr/bin 之前包含它)。您无需执行任何操作即可将 brew 安装的应用程序放入您的 PATH 中,它已经存在了。

试试which python看看它指向哪里。这是 MacOS 默认解释器的示例。

➜ which python
/usr/bin/python
➜ python
Python 2.7.16 (default, Jun 18 2021, 03:23:53)
[GCC Apple LLVM 12.0.5 (clang-1205.0.19.59.6) [+internal-os, ptrauth-isa=deploy on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> ^D

如果想让python指向系统的Python2.x,可以删除/usr/local/bin/python。然后要使用 Python3,您需要指定 python3。 (位于 /usr/local/bin/

您可以列出 /usr/local/bin,其中包含指向实际二进制文件的符号链接。

➜ ll /usr/local/bin | grep python
lrwxr-xr-x  1 usr  admin    38B Sep  5 12:27 python3 -> ../Cellar/python@3.9/3.9.6/bin/python3
lrwxr-xr-x  1 usr  admin    45B Sep  5 12:27 python3-config -> ../Cellar/python@3.9/3.9.6/bin/python3-config
lrwxr-xr-x  1 usr  admin    40B Sep  5 12:27 python3.9 -> ../Cellar/python@3.9/3.9.6/bin/python3.9