是否需要在 brew switch
之前/之后运行 brew unlink
/brew link
?
根据此链接https://docs.brew.sh/Tips-N'-Tricks#activate-a-previously-installed-version-of-a-formula brew switch
激活以前安装的公式版本。
看起来我安装了一些“系统”python(它安装在 /usr/bin/python3
下),但不是用 brew 安装的,如果我执行 brew 就可以看到它unlink python@3.9
和 brew unlink python@3.7
:
python -V
Python 3.7.3
/usr/bin/python3 -V
Python 3.7.3
which python3
/usr/bin/python3
然后我切换到 python@3.9:
brew switch python@3.9 3.9.0_1
python -V
Python 3.9.0
然后我切换到 python@3.7:
brew switch python@3.7 3.7.9
python -V
Python 3.9.0
但它仍然显示 Python 3.9.0,ls -l/usr/local/bin | grep python3
也显示了 python3.9。
我还尝试在每次 brew switch 测试之前取消所有 brew python 包的链接:
brew unlink python@3.7 && brew unlink python@3.9
brew switch python@3.7 3.7.9
python -V
Python 3.7.3
brew unlink python@3.7 && brew unlink python@3.9
brew switch python@3.9 3.9.0_1
python -V
Python 3.9.0
所以似乎出于某种原因它会自动链接 brew switch 上的 python@3.9 而不会为 python@3.7 这样做,为什么? brew switch
里面有 brew unlink
/brew link
还是应该手动完成?
我可以只执行 brew unlink
/brew link
来切换 python 版本吗?
更新:
brew
的较新版本似乎没有开关
:
Error: Calling `brew switch` is disabled! Use `brew link` @-versioned formulae instead.
brew --version
Homebrew 2.7.7
Homebrew/homebrew-core (git revision 918f0; last commit 2021-02-04)
Homebrew/homebrew-cask (git revision 2b83c; last commit 2021-02-04)
所以现在它应该是这样的:
brew unlink python@3.7 && brew unlink python@3.9
brew link python@3.7
python -V
Python 3.7.9
最佳答案
brew switch
不再是 brew 命令。
$ brew --version
Homebrew 3.0.0
Homebrew/homebrew-core (git revision 8d644; last commit 2021-02-10)
$ brew switch mariadb@10.3
Error: Unknown command: switch
Tips and Tricks您在问题中提到的页面也删除了对 brew switch
的任何提及。
Installing previous versions of formulae
The supported method of installing specific versions of some formulae is to see if there is a versioned formula (e.g. gcc@7) available. If the version you’re looking for isn’t available, consider using brew extract.
或者,man brew
中没有提到 brew switch
。
看来您的问题已由 Homebrew 维护者在上游解决。
注意 brew switch
自 Homebrew 2.6.0 以来已被弃用.
==更新==
如果我们以 python@3.7 和 python@3.9 为例。我们应该首先检查 brew info python@3.7
和 brew info python@3.9
的输出。我们看到 python@3.7 是 keg-only :
python@3.7 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.
这不是 python@3.9 的情况,因为它现在是 python 公式的别名。这意味着安装 python@3.9 会自动运行 brew link python@3.9
而安装 python@3.7 不会运行 brew link python@3.7
。
然后要列出哪些文件将与 python@3.9 链接,您可以运行 brew link --dry-run python@3.9
Would link:
/usr/local/bin/2to3
/usr/local/bin/2to3-3.9
/usr/local/bin/easy_install-3.9
/usr/local/bin/idle3
/usr/local/bin/idle3.9
/usr/local/bin/pip3
/usr/local/bin/pip3.9
/usr/local/bin/pydoc3
/usr/local/bin/pydoc3.9
/usr/local/bin/python3
/usr/local/bin/python3-config
/usr/local/bin/python3.9
/usr/local/bin/python3.9-config
/usr/local/bin/wheel3
/usr/local/share/man/man1/python3.1
/usr/local/share/man/man1/python3.9.1
/usr/local/lib/pkgconfig/python-3.9-embed.pc
/usr/local/lib/pkgconfig/python-3.9.pc
/usr/local/lib/pkgconfig/python3-embed.pc
/usr/local/lib/pkgconfig/python3.pc
/usr/local/Frameworks/Python.framework/Headers
/usr/local/Frameworks/Python.framework/Python
/usr/local/Frameworks/Python.framework/Resources
/usr/local/Frameworks/Python.framework/Versions/3.9
/usr/local/Frameworks/Python.framework/Versions/Current
要列出哪些文件将与 python@3.7 链接,请运行 brew link --dry-run python@3.7
:
Would link:
/usr/local/bin/2to3
/usr/local/bin/2to3-3.7
/usr/local/bin/easy_install-3.7
/usr/local/bin/idle3
/usr/local/bin/idle3.7
/usr/local/bin/pip3
/usr/local/bin/pip3.7
/usr/local/bin/pydoc3
/usr/local/bin/pydoc3.7
/usr/local/bin/python3
/usr/local/bin/python3-config
/usr/local/bin/python3.7
/usr/local/bin/python3.7-config
/usr/local/bin/python3.7m
/usr/local/bin/python3.7m-config
/usr/local/bin/pyvenv
/usr/local/bin/pyvenv-3.7
/usr/local/bin/wheel3
/usr/local/share/man/man1/python3.1
/usr/local/share/man/man1/python3.7.1
/usr/local/lib/pkgconfig/python-3.7.pc
/usr/local/lib/pkgconfig/python-3.7m.pc
/usr/local/lib/pkgconfig/python3.pc
/usr/local/Frameworks/Python.framework/Headers
/usr/local/Frameworks/Python.framework/Python
/usr/local/Frameworks/Python.framework/Resources
/usr/local/Frameworks/Python.framework/Versions/3.7
/usr/local/Frameworks/Python.framework/Versions/Current
所以为了回答你的问题,在 python@3.7 和 python@3.9 之间切换的一种方法是使用 brew link
和 brew unlink
是的,如果你有与 python@3.7 兼容但不与 python@3.9 兼容的脚本,反之亦然。
还有一种方法可以同时使用不同版本的python。这在 brew link --dry-run python@3.7
的输出末尾进行了解释:
If you need to have this software first in your PATH instead consider running:
echo 'export PATH="/usr/local/opt/python@3.7/bin:$PATH"' >> ~/.zshrc
对于需要python@3.7的脚本,可以设置:
export PATH="/usr/local/opt/python@3.7/bin
对于需要python@3.9的脚本,你可以设置:
export PATH="/usr/local/opt/python@3.9/bin
希望这对您有所帮助。
关于python-3.x - 您需要在 brew 切换之前/之后运行 brew unlink/brew link 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64833482/