安装 python 3.10 后无法运行 Ansible

use*_*955 0 python macos ansible

突然我无法运行ansible。

每当我尝试任何 ansible 命令,如ansible-playbook、ansible version等时,它都会显示错误

`/Users/myusername/Library/Python/2.7/bin/ansible: /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/: bad interpreter: No such file or directory`, 
Run Code Online (Sandbox Code Playgroud)

即使我让 ansible 运行不同的 python 版本,例如ansible-playbook --version -e 'ansible_python_interpreter=/usr/bin/python3,它也会显示相同的错误。

仅供参考:which ansible退货/Users/myusername/Library/Python/2.7/bin/ansible

估计和我最近安装的python有关。由于我最近安装了python3.10,所以python2.7变得无法工作。注意我自己并没有删除任何有关 python2.7 的内容。看起来 python3.10 的安装改变了 python2.7 设置。

对于大多数其他应用程序,我现在指示系统使用 python3 作为解决方法,例如我设置CLOUDSDK_PYTHON=/usr/bin/python3 为使访问云 cli 再次工作。但对于 Ansible,我还没有弄清楚如何让它发挥作用。我正在使用 MacOS 终端。

有谁知道如何解决上述问题以便我可以再次运行ansible?无论如何,只要我能运行 ansible 就可以了。要么重新安装ansible并让它使用python3而不是使用Python2.7,要么指导我如何重新安装python2.7(以正确的方式,不要与python3.10搞乱,目前我害怕安装python,我担心如果我重新安装python2.7,python3.10和2.7都将无法工作然后我无法工作)。

请帮忙。谢谢。

wkl*_*wkl 5

您必须对系统进行一些清理,因为不知何故,Python 2.7 已从您的系统中删除。这可能是由于在某个时候更新到 macos 12.4 Monterey 造成的,因为Monterey 12.3 及更高版本删除了系统提供的 Python 2.7,并将其替换为 Python 3.8.9 安装 ( /usr/bin/python3)。


但是,您的环境中仍然有一些引用所有 Python 2.7 内容的内容,例如您的${HOME}/Library/Python/2.7/bin/ansible目录。

您可以采取以下措施(希望)使ansible您的环境再次正常运行。

  1. 更改 shell 的PATH环境。

    您可能正在使用它,zsh因为它是 macOS 上的默认 shell。您是否曾经更改过您的.zshrc或其他环境文件以添加/Users/<name>/Library/Python/2.7/bin到您的PATH?您需要将其删除。

    此外,如果您严格想要使用手动安装的Python 3.10(而不是蒙特利的系统提供的/usr/bin/python3)python v3.8.9,您可能需要一个PATH看起来像这样的...

    # somewhere in your ~/.zshrc, probably near the bottom
    export PATH="/Library/Frameworks/Python.framework/Versions/3.10/bin:${PATH}"
    
    Run Code Online (Sandbox Code Playgroud)
  2. 重新安装 Ansible

    无论python3您使用什么二进制文件,请重新安装ansible

    python3 -m pip install --user ansible
    
    Run Code Online (Sandbox Code Playgroud)

    这最终将安装ansible到${HOME}/Library/Python/<VERSION>/bin

  3. 再次更新PATH以包含新bin目录

    在上面第 (1) 部分的基础上,您希望将binPython 内容的目录包含在您的用户目录中,以便能够引用pip install --user.

    # somewhere in your ~/.zshrc, probably near the bottom
    export PATH="${HOME}/Library/Python/3.10/bin:${PATH}"
    export PATH="/Library/Frameworks/Python.framework/Versions/3.10/bin:${PATH}"
    
    Run Code Online (Sandbox Code Playgroud)
  4. 重新加载 shell 并尝试运行ansible

    # if you already have a shell open, run this
    # to reload your zsh configs
    exec zsh
    
    # hopefully this returns the correct path!
    which ansible
    
    # and hopefully this runs!
    ansible --help
    
    Run Code Online (Sandbox Code Playgroud)
  5. 重新安装您需要的任何其他内容

    您引用的内容CLOUDSDK_PYTHON听起来像是您还gcloud安装了类似模块之类的内容。是时候用新的 Python 重新安装它们了。

    python3 -m pip install --user gcloud
    # and whatever else needs reinstalling
    
    Run Code Online (Sandbox Code Playgroud)

希望这一切都能修复您的环境。现在,一旦其他一切正常,您就可以从主目录中清理 Python 2.7 的残留内容,因为该目录已经损坏了引用已删除系统 Python 的模块安装。

cd ~/Library/Python
rm -rf 2.7
Run Code Online (Sandbox Code Playgroud)