2023-09-16 编辑
从brew版本开始4.1.0 https://brew.sh/2023/07/20/homebrew-4.1.0/(2023-07-20发布)有一个新命令称为pyenv-sync https://docs.brew.sh/Manpage#pyenv-sync
$ brew pyenv-sync --help
Usage: brew pyenv-sync
Create symlinks for Homebrew's installed Python versions in ~/.pyenv/versions.
Note that older patch version symlinks will be created and linked to the minor
version so e.g. Python 3.11.0 will also be symlinked to 3.11.3.
-d, --debug Display any debugging information.
-q, --quiet Make some output more quiet.
-v, --verbose Make some output more verbose.
-h, --help Show this message.
虽然我还没有尝试过,但这听起来像是将 Homebrew 的 Python 与 pyenv 链接的最佳选择。
编辑结束
不幸的是“取消链接”python
从所有公式来看,这有点不必要,因为每次更新您都必须“重新链接”它们。
Take black https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/black.rb, 举个例子:
$ ls -al $(brew --prefix black)/libexec/bin
total 104
drwxr-xr-x 16 thecesrom staff 512 Jun 11 08:32 .
drwxr-xr-x 6 thecesrom staff 192 Jun 11 08:32 ..
-rw-r--r-- 1 thecesrom staff 8834 Jun 10 15:27 Activate.ps1
-rw-r--r-- 1 thecesrom staff 1916 Jun 11 08:32 activate
-rw-r--r-- 1 thecesrom staff 865 Jun 11 08:32 activate.csh
-rw-r--r-- 1 thecesrom staff 2005 Jun 11 08:32 activate.fish
-rwxr-xr-x 1 thecesrom staff 256 Jun 11 08:32 black
-rwxr-xr-x 1 thecesrom staff 251 Jun 11 08:32 black-primer
-rwxr-xr-x 1 thecesrom staff 257 Jun 11 08:32 blackd
-rwxr-xr-x 1 thecesrom staff 1000 Jun 11 08:32 chardetect
-rwxr-xr-x 1 thecesrom staff 257 Jun 11 08:32 pip
-rwxr-xr-x 1 thecesrom staff 257 Jun 11 08:32 pip3
-rwxr-xr-x 1 thecesrom staff 257 Jun 11 08:32 pip3.9
lrwxr-xr-x 1 thecesrom staff 84 Jun 10 15:27 python -> ../../../../../opt/[email protected] /cdn-cgi/l/email-protection/Frameworks/Python.framework/Versions/3.9/bin/python3.9
lrwxr-xr-x 1 thecesrom staff 84 Jun 10 15:27 python3 -> ../../../../../opt/[email protected] /cdn-cgi/l/email-protection/Frameworks/Python.framework/Versions/3.9/bin/python3.9
lrwxr-xr-x 1 thecesrom staff 84 Jun 10 15:27 python3.9 -> ../../../../../opt/py[email protected] /cdn-cgi/l/email-protection/Frameworks/Python.framework/Versions/3.9/bin/python3.9
请注意python
, python3
and python3.9
全部指向 Homebrew 的python
依赖性。所以每次公式都取决于python
已更新,您必须unlink
所有符号链接并重新创建它们指向您的pyenv
-安装版本。但这仍然无法删除python
通过下载brew
,并且您最终可能会在计算机上安装两次相同的 Python 版本。
查看此问题的另一种方法是添加brew
's python
to pyenv
.
正如你所看到的,就我而言[email protected] /cdn-cgi/l/email-protection
作为依赖项安装black
和其他公式,所以这就是我所做的。
首先,创建一个symlink
at ~/.pyenv/versions
.
$ cd ~/.pyenv/versions
$ ln -sfv "$(brew --prefix [email protected] /cdn-cgi/l/email-protection)" 3.9
$ ls -al
total 0
drwxr-xr-x 4 thecesrom staff 128 Aug 31 07:51 .
drwxr-xr-x 6 thecesrom staff 192 Jun 8 10:58 ..
drwxr-xr-x 6 thecesrom staff 192 Aug 31 07:48 2.7.18
lrwxr-xr-x 1 thecesrom staff 25 Aug 31 07:51 3.9 -> /usr/local/opt/[email protected] /cdn-cgi/l/email-protection
2021-09-06 编辑:我建议使用3.9
以避免每次 Python 升级时都必须更改符号链接。
因为我想包含相同的文件夹(bin
, include
, lib
, share
)作为我的其他 Python 安装(2.7.18
),我通过安装pyenv
,然后我创建了一个符号链接include
.
$ cd "$(brew --prefix [email protected] /cdn-cgi/l/email-protection)"
$ ln -sfv Frameworks/Python.framework/Versions/3.9/include/python3.9 include
include -> Frameworks/Python.framework/Versions/3.9/include/python3.9
一旦我这样做了,我就创建了symlinks
for idle
, pip
, python
and wheel
在以下位置:
$ cd "$(brew --prefix [email protected] /cdn-cgi/l/email-protection)/bin"
$ ln -sfv idle3 idle
idle -> idle3
$ ln -sfv pip3 pip
pip -> pip3
$ ln -sfv python3 python
python -> python3
$ ln -sfv wheel3 wheel
wheel -> wheel3
我这样做是因为我运行时得到以下输出brew link [email protected] /cdn-cgi/l/email-protection
:
$ brew link [email protected] /cdn-cgi/l/email-protection
Warning: Already linked: /usr/local/Cellar/[email protected] /cdn-cgi/l/email-protection/3.9.7
To relink, run:
brew unlink [email protected] /cdn-cgi/l/email-protection && brew link [email protected] /cdn-cgi/l/email-protection
完成后,运行pyenv rehash
根据推荐pyenv
每当您安装新版本时。
$ pyenv rehash
然后,验证由以下人员管理的所有版本pyenv
.
$ pyenv versions
system
2.7.18
* 3.9 (set by /Users/thecesrom/.pyenv/version)
最后配置pyenv
如所须。就我而言,我设置3.9
通过运行作为全局pyenv global 3.9
,现在当我跑步时python --version
我得到以下输出:
$ python --version
Python 3.9.7