简体   繁体   English

得到 Ansible 错误的 Python 解释器错误?

[英]getting Ansible bad python interpreter error?

I installed ansible on MAC High Sierra 10.13.3 and when I am trying to run我在 MAC High Sierra 10.13.3 上安装了 ansible,当我尝试运行时

"ansible --version" I am receiving following error “ansible --version”我收到以下错误

-bash: /usr/local/bin/ansible: /usr/local/opt/python/bin/python2.7: bad interpreter: No such file or directory -bash: /usr/local/bin/ansible: /usr/local/opt/python/bin/python2.7: bad interpreter: 没有那个文件或目录

Please let me know if you have ran into same issue or have solution.如果您遇到相同的问题或有解决方案,请告诉我。

/usr/local/bin/ansible has PATH "/usr/local/opt/python/bin/python2.7" on the first line. /usr/local/bin/ansible在第一行有 PATH "/usr/local/opt/python/bin/python2.7" and in /usr/local/opt/python/bin/ directory I had python3.6 instead of python2.7./usr/local/opt/python/bin/目录中,我有 python3.6 而不是 python2.7。

So I changed PATH on file vi /usr/local/bin/ansible所以我在文件vi /usr/local/bin/ansible上更改了 PATH

from #!/usr/local/opt/python/bin/python2.7 to #!/usr/local/opt/python/bin/python3.6 and that fixed the issue#!/usr/local/opt/python/bin/python2.7#!/usr/local/opt/python/bin/python3.6并解决了问题

Verification :验证:

$ ansible --version
  ansible 2.5.0
  config file = None
  configured module search path = ['/Users/<username>/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/<username>/Library/Python/3.6/lib/python/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.6.4 (default, Mar  1 2018, 18:36:50) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]


$ ansible localhost -m ping

  localhost | SUCCESS => {
    "changed": false,
    "ping": "pong"
  }


$ ansible localhost -m setup -a 'filter=ansible_distribution' 
  localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_distribution": "MacOSX"
    },
    "changed": false
  }

In my case I am using pyenv to manage my python versions and $PATH and symlinks were all correct pointing to the correct paths.在我的情况下,我使用pyenv来管理我的 python 版本和 $PATH 和符号链接都是正确的指向正确的路径。

Check your python paths检查你的python路径

$ pyenv which python
  /Users/<username>/.pyenv/versions/3.7.3/bin/python
$ which python
  /Users/<username>/.pyenv/shims/python

Check ansible configuration检查ansible配置

ansible configuration at /usr/local/bin/ansible pointed the correct python version 3.7 /usr/local/bin/ansible ansible 配置指出了正确的 python 版本 3.7

#!/usr/local/opt/python/bin/python3.7
.
.
.

but ansible --version returned python 2.7 as its interpreter但是ansible --version返回了 python 2.7 作为它的解释器

$ ansible --version
  ansible 2.9.12
  configured module search path = ['/Users/<username>/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/<username>/.pyenv/versions/2.7.16/lib/python2.7/site-packages/ansible
  executable location = /Users/<username>/.pyenv/versions/2.7.16/bin/ansible
  python version = 2.7.16 (default, Apr  2 2020, 13:02:51) [Clang 11.0.3 (clang-1103.0.32.29)]

Ansible for Python3适用于 Python3 的 Ansible

Official ansible docs said to use pip3 to install ansible for python3 and I uninstalled ansible and reinstalled using pip3 but the interperter still pointed to python2.7. ansible 官方文档说使用pip3为python3安装ansible,我卸载ansible并使用pip3重新安装但interperter仍然指向python2.7。

Finally I manually added .ansible.cfg file in my home path and configured python interpreted manually by adding最后,我在主路径中手动添加了.ansible.cfg文件,并通过添加手动解释了配置的 python

  ansible_python_interpreter=/usr/bin/python

Example config file for ansible.cfg ansible.cfg 的示例配置文件

Now ansible is configured for python3 correctly现在ansible已正确配置为python3

ansible 2.9.12
  config file = /Users/<username>/.ansible.cfg
  configured module search path = ['/Users/<username>/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/<username>/.pyenv/versions/3.7.3/lib/python3.7/site-packages/ansible
  executable location = /Users/<username>/.pyenv/versions/3.7.3/bin/ansible
  python version = 3.7.3 (default, Apr  2 2020, 13:02:51) [Clang 11.0.3 (clang-1103.0.32.29)]

Changing the python version might be pushing into some compatibility issues更改 python 版本可能会导致一些兼容性问题

It happens, when we have multiple python versions installed in our OS.当我们的操作系统中安装了多个 python 版本时,就会发生这种情况。

Simple steps for troubleshooting:故障排除的简单步骤:

  1. Check the python version command: which python /usr/bin/python检查python版本命令: which python /usr/bin/python
  2. Create a soft link to the path command : ln -s /usr/bin/python /usr/local/opt/python/bin/python2.7创建路径命令的软链接: ln -s /usr/bin/python /usr/local/opt/python/bin/python2.7

I hope it will fix the error.我希望它会修复错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM