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.
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 yourPATH
? 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 ispython v3.8.9
), you will probably need aPATH
that looks like this...# somewhere in your ~/.zshrc, probably near the bottom export PATH="/Library/Frameworks/Python.framework/Versions/3.10/bin:${PATH}"
Re-install Ansible
With whatever
python3
binary you're using, re-installansible
python3 -m pip install --user ansible
This will end up installing
ansible
into${HOME}/Library/Python/<VERSION>/bin
Update
PATH
again to include newbin
dirBuilding 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 bypip 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}"
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
Reinstall Anything Else You Need
You reference stuff like
CLOUDSDK_PYTHON
which sounds like you also have stuff like thegcloud
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