如果已经安装了多个版本(包括升级版),如何在 mac 上升级 python?

How to upgrade python on mac if multiple versions (including upgraded version) are already installed?

我使用命令 pip3 install --upgrade pip 升级了 pip;然后,在终端 window:

中打印了以下内容
DEPRECATION: Python 3.5 reached the end of its life on September 13th,
2020. Please upgrade your Python as Python 3.5 is no longer maintained. pip 21.0 will drop support for Python 3.5 in January 2021.
pip 21.0 will remove support for this functionality.

在线搜索 python 在 mac 上升级的正确方法让我找到了两个来源(1) and (2). The second source mentions modifying the .profile and bash script; these are both things I am having trouble understanding, despite some help from similar questions on these stack exchange forums like and that。但是,使用第一个来源的建议,我想我'我已经确定了一个潜在的未来问题(对此我阅读了 virtual venv 可能是一个很好的解决方案)。具体来说,

$ which python 
/usr/bin/python

$ which python3
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3

$ brew info python
python@3.8: stable 3.8.5 (bottled)
Interpreted, interactive, object-oriented programming language
https://www.python.org/
/usr/local/Cellar/python@3.8/3.8.5 (4,331 files, 66.8MB)
  Poured from bottle on 2020-09-23 at 04:16:21
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/python@3.8.rb
License: Python-2.0
==> Dependencies
Build: pkg-config ✔
Required: gdbm ✔, openssl@1.1 ✔, readline ✔, sqlite ✔, xz ✔
==> Caveats
Python has been installed as
  /usr/local/bin/python3

Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
  /usr/local/opt/python@3.8/libexec/bin

You can install Python packages with
  pip3 install <package>
They will install into the site-package directory
  /usr/local/lib/python3.8/site-packages

See: https://docs.brew.sh/Homebrew-and-Python
==> Analytics
install: 435,972 (30 days), 1,606,776 (90 days), 3,198,770 (365 days)
install-on-request: 158,335 (30 days), 466,841 (90 days), 519,965 (365 days)
build-error: 0 (30 days)

我认为这意味着我在 mac 上安装了 python 2、python 3.5 和 python 3.8。我尝试使用以下命令的多个版本(即 pythonpython3python3.8 等)升级我的 python 版本:

$ pip3 install --upgrade python
ERROR: Could not find a version that satisfies the requirement python (from versions: none)
ERROR: No matching distribution found for python

我已经安装了 python 3.8,但我显然是 运行ning 3.5;如何升级我的 python 版本?我该如何“清理这个烂摊子”?如果相关,我是 运行ning macOS High Sierra 10.13.6,我 like/use 唯一的文本编辑器是 Atom(请不要就 vim).

编辑#1:

关注the steps from this post后,以下是在TextEdit中打开的

# Setting PATH for Python 3.5
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"
export PATH

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

我注意到它在顶部显示 Python 3.5。我是否用 PATH=/usr/local/bin 覆盖给定路径 PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"?我要运行python3.8;这会做我想要的吗?

编辑#2:

如编辑后的答案中所建议,终端中的 运行ning grep PATH $HOME/.* 在终端 window 中输出以下内容:

grep: /Users/username/.bash_sessions: Is a directory
grep: /Users/username/.cache: Is a directory
grep: /Users/username/.config: Is a directory
grep: /Users/username/.cups: Is a directory
grep: /Users/username/.git: Is a directory
grep: /Users/username/.idlerc: Is a directory
grep: /Users/username/.lightkurve-cache: Is a directory
grep: /Users/username/.local: Is a directory
grep: /Users/username/.matplotlib: Is a directory
grep: /Users/username/.npm: Is a directory
grep: /Users/username/.nvm: Is a directory
grep: /Users/username/.oracle_jre_usage: Is a directory
grep: /Users/username/.ssh: Is a directory
grep: /Users/username/.subversion: Is a directory
grep: /Users/username/.vnc: Is a directory

因此,我搜索包含 python 3.5 的目录。如果我在 /Users/username/Library/ls,则 Frameworks 未列出,因此我在其他地方搜索。但这让我担心,因为我看到 PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"(来自编辑 #1)。

$ pwd
/Users/username

$ cd .local/
$ ls 
share

$ cd share
$ ls
virtualenvs

$ cd virtualenvs
$ ls
PyCodes-_a3khG54    

$ cd PyCodes-_a3khG54
$ ls
bin include lib src

目录src为空; include 包含 python3.5mlib 包含 python3.5

$ cd bin
$ ls

activate        f2py            python
activate.csh        f2py3           python-config
activate.fish       f2py3.5         python3
activate_this.py    pip         python3.5
easy_install        pip3            wheel
easy_install-3.5    pip3.5

这里不应该有我可以编辑的文件吗?

您显示的输出来自:

brew info python

告诉你答案!

如果我解释一下,它会说:

If you want to run Python 3.8 and pip by typing python3 and pip3, you need to put /usr/local/bin first on your PATH. If, instead, you want to run them by typing python or pip, i.e. without a version, you need to put /usr/local/opt/python@3.8/libexec/bin first on your PATH.

要说在哪里更改您的 PATH 并不是那么简单,因为关于它的设置位置有很多可能性。您最好的选择可能是查看主目录中的所有 "dotfiles",因此:

grep PATH $HOME/.*

然后你需要打开最有可能的文件,并在最后设置的位置之后添加,比如:

export PATH=/usr/local/bin:$PATH

要真正激活它,最简单的可能是启动一个新的终端和 运行:

echo $PATH

如果看起来正确,请使用旧的 PATH 关闭终端,否则您会感到困惑。如果它看起来不正确或导致错误,请立即关闭新终端 window 并在原始终端中重试编辑。


如果你想知道你在输入 pythonpip 时 运行 的实际身份,运行:

type python
type pip

如果你想知道你在输入 python3pip3 时 运行 的实际身份,运行:

type python3
type pip3

您可以像这样解释最后几个 type 命令的输出:

  • 如果它们在开头附近包含单词 local,可能是 /usr/local/XXX,它们很可能是由 homebrew 或您提供的自己编译或安装,

  • 如果它们以 /Library 开头或包含单词 Frameworks,它们可能是由 Apple 提供的并且是最近的,

  • 如果它们以 /usr/bin 开头,但 没有 单词 local,它们可能是较旧的 Apple-supplied 二进制文件,

  • 如果它说你的命令是 "aliased",这意味着你在 的某处有一个像 alias python="something crazy" 这样的命令“点文件”(见上文)。就个人而言,我会删除它,因为它只是增加了另一层间接和混乱...... YMMV on this one.

切勿删除或更改 Apple 提供的任何内容 - 否则将来 macOS 升级可能会失败。


因此,希望我们已经确定您需要使用 pythonpip,或者使用 python3pip3,并且它们应该匹配,即当你安装一个包时,你将它安装在你使用的 Python 解释器可用的地方。因此,“匹配” 安装示例如下所示:

type python3
python3 is /usr/local/bin/python3

python3 -V
Python 3.8.5

pip3 -V
pip 20.1.1 from /usr/local/lib/python3.8/site-packages/pip (python 3.8)

type pip3
pip3 is hashed (/usr/local/bin/pip3)

请注意我的两个二进制文件的名称中都包含 local,因此它们都来自 homebrew 以及 Python 版本与 pip3 -V 命令中的版本相匹配,因此我可以确定我正在为我的 Python 解释器安装正确的位置。


为什么很难说在哪里编辑您的 PATH?答案是这取决于很多事情。这取决于您的 shell,因此您可能会看到 运行 echo $SHELL 的建议,但这只会告诉您默认的 shell,而不是当前的 shell,你可以用 ps -o comm= $$ 找到它。但是您的登录 shell 可能是 bash 并且您可能在 bash 个人资料中执行了 fish。或者您可以使用 iTerm2Terminalxterm 并且您可能已在该终端的 “首选项” 中更改了 shell程序。