切换 Homebrew 安装的 Python 版本 (3.9 → 3.8)

Switching Python version (3.9 → 3.8) installed by Homebrew

这与 的情况非常相似,但反之亦然。我通过 Homebrew 安装了 Python 3.8 并将其更新为 3.9:

% brew list --formula | grep python
python@3.8
python@3.9

我想使用 Python 3.8 作为带有 python3 命令的默认版本,并尝试了 - 受 启发 - 以下内容:

brew unlink python@3.9
brew unlink python@3.8
brew link python@3.8

最后给了我以下输出:

% brew link python@3.8
Linking /usr/local/Cellar/python@3.8/3.8.6_2... 
Error: Could not symlink bin/pip3
Target /usr/local/bin/pip3
already exists. You may want to remove it:
  rm '/usr/local/bin/pip3'

To force the link and overwrite all conflicting files:
  brew link --overwrite python@3.8

To list all files that would be deleted:
  brew link --overwrite --dry-run python@3.8

所以我做到了:

% brew link --overwrite --dry-run python@3.8
Would remove:
/usr/local/bin/pip3

If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/usr/local/opt/python@3.8/bin:$PATH"' >> ~/.zshrc

我认为最好先检查一下:

ralf@razbook ~ % brew link --force --dry-run python@3.8
Would link:
/usr/local/bin/2to3
/usr/local/bin/2to3-3.8
/usr/local/bin/easy_install-3.8
/usr/local/bin/idle3
/usr/local/bin/idle3.8
/usr/local/bin/pip3
/usr/local/bin/pip3.8
/usr/local/bin/pydoc3
/usr/local/bin/pydoc3.8
/usr/local/bin/python3
/usr/local/bin/python3-config
/usr/local/bin/python3.8
/usr/local/bin/python3.8-config
/usr/local/bin/wheel3
/usr/local/share/man/man1/python3.1
/usr/local/share/man/man1/python3.8.1
/usr/local/lib/pkgconfig/python-3.8-embed.pc
/usr/local/lib/pkgconfig/python-3.8.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.8
/usr/local/Frameworks/Python.framework/Versions/Current

If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/usr/local/opt/python@3.8/bin:$PATH"' >> ~/.zshrc

听起来不错,让我们开始吧:

 % brew link --force python@3.8 
Linking /usr/local/Cellar/python@3.8/3.8.6_2... 
Error: Could not symlink bin/pip3
Target /usr/local/bin/pip3
already exists. You may want to remove it:
  rm '/usr/local/bin/pip3'

To force the link and overwrite all conflicting files:
  brew link --overwrite python@3.8

To list all files that would be deleted:
  brew link --overwrite --dry-run python@3.8

可惜我跳过了dry-运行:

 % brew link --overwrite python@3.8
Linking /usr/local/Cellar/python@3.8/3.8.6_2... 25 symlinks created

If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/usr/local/opt/python@3.8/bin:$PATH"' >> ~/.zshrc

有些事情似乎奏效了:

% python3 --version
Python 3.8.6
% pip3 --version
pip 20.2.4 from /usr/local/lib/python3.8/site-packages/pip (python 3.8)

但是 pipenv 还是有问题:

% pipenv install google-ads
Warning: Python 3.9 was not found on your system...
Neither 'pyenv' nor 'asdf' could be found to install Python.
You can specify specific versions of Python with:
$ pipenv --python path/to/python

也许我应该重新安装 pipenv?

% which pipenv
/usr/local/bin/pipenv

% pip3 uninstall pipenv
Found existing installation: pipenv 2020.8.13
Uninstalling pipenv-2020.8.13:
  Would remove:
    /usr/local/bin/pipenv
    /usr/local/bin/pipenv-resolver
    /usr/local/lib/python3.8/site-packages/pipenv-2020.8.13.dist-info/*
    /usr/local/lib/python3.8/site-packages/pipenv/*
Proceed (y/n)? y
  Successfully uninstalled pipenv-2020.8.13

% pip3 install pipenv
Collecting pipenv
  Downloading pipenv-2020.11.15-py2.py3-none-any.whl (3.9 MB)
     |████████████████████████████████| 3.9 MB 2.9 MB/s 
Requirement already satisfied: pip>=18.0 in /usr/local/lib/python3.8/site-packages (from pipenv) (20.2.4)
Requirement already satisfied: virtualenv in /usr/local/lib/python3.8/site-packages (from pipenv) (20.0.31)
Requirement already satisfied: setuptools>=36.2.1 in /usr/local/lib/python3.8/site-packages (from pipenv) (50.3.2)
Requirement already satisfied: certifi in /usr/local/lib/python3.8/site-packages (from pipenv) (2020.6.20)
Requirement already satisfied: virtualenv-clone>=0.2.5 in /usr/local/lib/python3.8/site-packages (from pipenv) (0.5.4)
Requirement already satisfied: distlib<1,>=0.3.1 in /usr/local/lib/python3.8/site-packages (from virtualenv->pipenv) (0.3.1)
Requirement already satisfied: filelock<4,>=3.0.0 in /usr/local/lib/python3.8/site-packages (from virtualenv->pipenv) (3.0.12)
Collecting six<2,>=1.9.0
  Using cached six-1.15.0-py2.py3-none-any.whl (10 kB)
Requirement already satisfied: appdirs<2,>=1.4.3 in /usr/local/lib/python3.8/site-packages (from virtualenv->pipenv) (1.4.4)
Installing collected packages: pipenv, six
Successfully installed pipenv-2020.11.15 six-1.15.0

但仍然:

% pipenv install google-ads
Warning: Python 3.9 was not found on your system...
Neither 'pyenv' nor 'asdf' could be found to install Python.
You can specify specific versions of Python with:
$ pipenv --python path/to/python

实际上不行,但让我们声明 Python pipenv 应该使用:

% pipenv --python /usr/local/opt/python@3.8/bin/python3 install google-ads
Creating a virtualenv for this project...
Pipfile: /Users/ralf/code/test_snippets/20-12-10_google_ads/Pipfile
Using /usr/local/opt/python@3.8/bin/python3 (3.8.6) to create virtualenv...
⠦ Creating virtual environment...created virtual environment CPython3.8.6.final.0-64 in 362ms
  creator CPython3Posix(dest=/Users/ralf/.local/share/virtualenvs/20-12-10_google_ads-S7vGVfKj, clear=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/ralf/Library/Application Support/virtualenv)
    added seed packages: pip==20.2.4, setuptools==50.3.2, wheel==0.35.1
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

✔ Successfully created virtual environment! 
Virtualenv location: /Users/ralf/.local/share/virtualenvs/20-12-10_google_ads-S7vGVfKj
Warning: Your Pipfile requires python_version 3.9, but you are using 3.8.6 (/Users/ralf/.local/share/v/2/bin/python).
  $ pipenv --rm and rebuilding the virtual environment may resolve the issue.
  $ pipenv check will surely fail.
Warning: Your Pipfile requires python_version 3.9, but you are using 3.8.6 (/Users/ralf/.local/share/v/2/bin/python).
  $ pipenv --rm and rebuilding the virtual environment may resolve the issue.
  $ pipenv check will surely fail.
Installing google-ads...

pipenv --rm 并且重建虚拟环境没有帮助。 (我什至考虑简单地卸载 Python 3.9 并再次安装 Python 3.8 – 但是 don't know how。)

有没有办法说服 pipenv 使用 Python 3.8?

好吧,有时候提出问题有助于自己找到解决方案 – 顺便说一下,Whosebug 的一大优点。

提示在 pipenv 的警告中:“您的 Pipfile 需要 python_version 3.9”。

我只是做了

rm Pipfile
rm Pipfile.lock

然后成功了:

pipenv install google-ads

好吧,至少 pipenv 在 Python 3.8 中可以正常工作。 google-ads 仍然存在问题,但那是

可能更改 Pipfile 就足够了:

[requires]
python_version = "3.8"

很高兴您能够以自己的方式解决问题,但让我添加一些建议。一般来说,降级 Python(或任何程序)通常不受支持。升级可能会升级其他包中的依赖项,这些依赖项不向后兼容 Python 的旧版本或其他依赖项。简而言之,您 不应该 甚至想要“降级”软件包。

相反,您应该使用多个独立的环境,例如使用 virtualenv 这样,如果您出于某种原因 需要 一个较低版本,您可以用一个新的较低版本替换整个环境(请注意与“降级”的细微差别,因为您正在完全使用新环境)。