Home > Net >  Can not run Ansible after installing python 3.10
Can not run Ansible after installing python 3.10

Time:07-27

Suddenly I cannot run ansible.

Whenever I try any ansible command like ansible-playbook, ansible version, etc. it shows error

`/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`, 

Even if I let ansible run different python version, like ansible-playbook --version -e 'ansible_python_interpreter=/usr/bin/python3, it shows the same error.

FYI: which ansible returns /Users/myusername/Library/Python/2.7/bin/ansible

I guess it is related to my recent installation of python. Since I installed a python3.10 recently, The python2.7 becomes not work. Note I did not remove anything about python2.7 myself. looks like the installation of python3.10 changed python2.7 setting.

For most other application, I now pointed the system to use python3 as workaround, e.g. I set CLOUDSDK_PYTHON=/usr/bin/python3 to make accessing cloud cli works again. But for Ansible, I have not figured out how to make it work. I am using MacOS terminal.

Does anybody know how to resolve the above issue so that I can run ansible again? Anyway is OK as long as I can run ansible. Either reinstall ansible and let it use python3 instead of using Python2.7 or guide me how to reinstall python2.7 (in a right way, not messy with python3.10, currently I am scared of installing python, I am afraid if I reinstall python2.7, both python3.10 and 2.7 will be out of work then I cannot work).

Please help. Thanks.

CodePudding user response:

You will have to do some cleanup on your system because somehow, Python 2.7 was removed from you system. This might have happened due to updating to macos 12.4 Monterey at some point, because Monterey 12.3 and newer removed the system-provided Python 2.7, replacing it with a Python 3.8.9 installation (/usr/bin/python3).


However, you still have stuff in your environment that reference all those Python 2.7 things, like your ${HOME}/Library/Python/2.7/bin/ansible directory.

Here are the things you can do to (hopefully) make ansible and your environment work again.

  1. Change your shell's PATH environment.

    You're probably using zsh since it's the default shell on macos. Have you ever changed your .zshrc or other environment files to add /Users/<name>/Library/Python/2.7/bin in your PATH? You will need to remove that.

    Additionally, if you strictly want to use the Python 3.10 you manually installed (and not Monterey's system-provided /usr/bin/python3 which is python v3.8.9), you will probably need a PATH that looks like this...

    # somewhere in your ~/.zshrc, probably near the bottom
    export PATH="/Library/Frameworks/Python.framework/Versions/3.10/bin:${PATH}"
    
  2. Re-install Ansible

    With whatever python3 binary you're using, re-install ansible

    python3 -m pip install --user ansible
    

    This will end up installing ansible into ${HOME}/Library/Python/<VERSION>/bin

  3. Update PATH again to include new bin dir

    Building on part (1) above, you want to include the bin directory for Python stuff in your user directory, to be able to refer to anything installed by 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}"
    
  4. Reload your shell and try to run 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
    
  5. Reinstall Anything Else You Need

    You reference stuff like CLOUDSDK_PYTHON which sounds like you also have stuff like the gcloud module installed. Time to reinstall those with your new Python.

    python3 -m pip install --user gcloud
    # and whatever else needs reinstalling
    

Hopefully this all fixes your environment. Now you can clean up the remnants of Python 2.7 stuff from your home directory once everything else is working, as this directory has broken module installs that reference a deleted system Python anyway.

cd ~/Library/Python
rm -rf 2.7
  • Related