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.
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 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}"
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
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}"
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 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
Just recently upgraded to Mac OSX Catalina, and it seems to have broken all my Python venv's.
I have a venv that I previously created before upgrading to Catalina.
I can activate the venv without issue: source venv/bin/activate
When I type python or python3, it actually runs the system Python interpreters, instead of the expected Python interpreter in my venv.
With my venv activated...
>>> which python
/usr/bin/python
>>> which python3
/usr/local/bin/python3
I expect that to point to /Users/<username>/<path-to-venv>/venv/bin/python3.
If I try to run that Python interpreter directly:
>>> /Users/<username>/<path-to-venv>/venv/bin/python3
bash: /Users/<username>/<path-to-venv>/venv/bin/python3: No such file or directory
If I navigate to this venv directory in Finder, I can see that the python3 file exists, although it's actually an alias. If I double-click on it, I get an error message that pops up that reads: The operation can't be completed because the original item for "Python3" can't be found.
So I think the way these venv's work is that they are based on references to existing Python interpreter installations. Evidently running a venv invokes these references, and if the original installation is broken it fails. I know that Catalina jacked with the default Python settings and went so far as to change installation directories.
Has anyone else encountered this? Can anyone offer tips to fix this? This seems to affect all my venv virtual environments across my system.
You can try to create a new venv in the same folder or to update your existing venv. With all the following I assume you've created your virtual environments with the bundled venv module
For the creation it's the command you've probably already used
python3 -m venv <path_to_dir>
In order to update, the --upgrade parameter is what you're looking for.
You need to run it with the targeted Python version, for example in this case:
python3 -m venv --upgrade <path_to_dir>
for more details, see my answer here
I'm trying to run the autopep8 linter on a Python file in VSCode.
I've followed the instructions here: https://code.visualstudio.com/docs/python/environments and selected my interpreter (⇧⌘P): /usr/local/bin/python
I then try to format my code, and VSCode says autopep8 isn't installed, and can be installed via Pip. However, when I try to install via Pip, it says There is no Pip installer available in the selected environment.
I then tried launching a terminal in the current environment by selecting Python: Create Terminal from the Command Palette.
The terminal opens fine, pip is present, and I'm even able to pip install autopep8 in the terminal that opened in VSCode, but when I try running the Format Document command I get the same errors that autopep8 and pip aren't available in the environment.
On Ubuntu16.04, I worked with Python3 in vscode and
apt-get install python3-pip
solves my problem.
That's because I discover that: Under my terminal, I type the pip -V. It displays it's for Python2, not for Python3.
Man you can only change the interpreter.
Go in (ctrl + shift + p), then type Python: Select Interpreter, this way you choose the version that your extension needs.
This worked for me.
I have multiple python versions:
2.7
3.6
3.7
Tell the vscode/ visual studio code, which version to use:
press the following (Show All Commands): Ctrl + Shift + P
paste the following: Python: Select Interpreter
Select one of the version that it shows, I have selected python 3.7.3 64-bit
Update python path in settings:
press Ctrl + , to open Settings
search for python.pythonPath
change python to /usr/bin/python3.7
Note: this may not be needed, however, make sure /usr/bin/python3.7 really exists for you, you may have at a different path like /usr/local/bin/python3.7, etc.
I had pip but it was 2.7, but since I am choosing python 3, it's pip needs to be installed
Run the following command in Terminal: apt-get install python3-pip
Restart vscode
With the above steps, all issues got resolved. Hope that helps.
try Ctrl+Shift+P
then type
Python: Select Interpreter
and select the python version.
Installing python3-pip fixed the issue for me.
apt-get install python3-pip
1.Select the File > Preferences > Settings command (⌘,) to open your User Settings.
2.Search and Create or modify an entry for python.pythonPath with the full path to the Python executable according to your requirements.For Example i changed it to python2.6 path to this path /usr/local/bin/python3.
I had the same problem today, none of the solutions helped me. Eventually, I figured it out myself.
I'm posting this answer for people who are having this problem. Just go to your ./venv folder and you will find a .cfg file.
Just make sure include-system-site-packages is set to true
home = /usr/bin
include-system-site-packages = true
version = 3.8.5
If it still doesn't work, just run sudo install python3-pip once in the terminal. Of course you could always change the version here as well.
(on mac)if you are using python3 but vscode told you pip was not installed , you could change python version on vscode bottom.
And I guess you are using another terminal but not bash , vscode's default terminal is bash. Bash don't know you have install pip#2.
For WSL users:
If you have your work files in linux subsystem path, you still need pip for windows in order to VSCode to use it.
On Ubuntu:
Make sure, that you have Python and pip installed.
Go to Settings, type python.py in search input. This should find Python Path settings.
Remove this path (if it is currently setted), save. Exit Code and set this to current value.
For me is /usr/bin/python3
Had this issue when trying use autopep8. For me - it had nothing to do with pip (I know it is installed) but the path the VScode python extension was using.
If you open up the extension and go to it's setting and scroll down, there is a path you can designate for autopep8: Python › Formatting: Autopep8 Path
I used the path recommended by these docs:
https://code.visualstudio.com/docs/python/editing#_formatting
Which was: python.formatting.autopep8Args
I stopped getting the error.
For Windows system check the environment variable>System variables
check the Path variable for the python path.(if not found set the path variable for python)
Copy the path and paste under vscode>file>preferences>settings>python.pythonPath
It worked for me.
I ran into this problem while learning django and the terminal would not let me pip install anything.
Create a virtual environment in shell and then use the path of the environment as your interpreter. This worked for me.
Note: You might want to create to create the environment in a different shell altogether and then upload the folder of the environment into vscode.
Then you open up the settings file in the environment folder.
This image will hopefully give you a good idea. Click Here
I was having a similar problem with pylint in a docker container. I realized that the reason the VS-Code-prompted pylint install didn't work for me was because I was using the global python installation (global inside my docker container, anyway), which can require elevated permissions to install things and VS code wasn't running as root in the container. According to the vs code python extension docs:
Note: If you're using a global environment and VS Code is not running elevated, linter installation may fail. In that case, either run VS Code elevated, or manually run the Python package manager to install the linter at an elevated command prompt for the same environment: for example sudo pip3 install pylint (macOS/Linux) or pip install pylint (Windows, at an elevated prompt)
Go in (ctrl + shift + p), then type Python: Select Interpreter,
then type Python: Select Interpreter and then click on "Enter interpreter path"
Then click on "Find.. Browse your file. "
Then type Python in c drive search bar and click on latest version of python in case if you have multiple version of python. Enter and modify wait for sometimes to complete then close.
After that restart your vs code.
It worked for me it will work for you also.
Note: This is a solution for Windows.
First make Sure your Python is installed properly. Run the following command:
py --version
If the Previous command is running fine it will give you your python version. In that case go ahead and check if pip is present or not. Run the below command to check:
py -m pip
If pip is present in your system it show give you a list of option and info. If that's the case then go Ahead and run pip. All you need to do is precede the command you would normally write with py -m. For example:
pip install flask
py -m pip install flask
This should solve your problem.
What I'm trying to do here is to make python3 as my default python. Except the python 2.7 which automatically installed on mac, I installed python3 with homebrew. This is the website that I'm following. http://docs.python-guide.org/en/latest/starting/install3/osx/#install3-osx
I guess I followed every instruction well, got xcode freshly installed, Command line tools, and homebrew. But here's my little confusion occurs.
The script will explain what changes it will make and prompt you before the installation begins. Once you’ve installed Homebrew, insert the Homebrew directory at the top of your PATH environment variable. You can do this by adding the following line at the bottom of your ~/.profile file
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
I was really confused what this was, but I concluded that I should just add this following line at the bottom of ~/.profile file. So I opened the ~/.profile file by open .profile in the terminal, and added following line at the bottom. And now it looks like this.
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
# Setting PATH for Python 3.6
# The original version is saved in .profile.pysave
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
And then I did brew install python, and was hoping to see python3 when I do python --version.
But it just shows me python 2.7.10. I want my default python to be python3 not 2.7
And I found a little clue from the website.
Do I have a Python 3 installed?
$ python --version
Python 3.6.4
If you still see 2.7 ensure in PATH /usr/local/bin/ takes pecedence over /usr/bin/
Maybe it has to do something with PATH? Could someone explain in simple English what PATH exactly is and how I could make my default python to be python3 when I run python --version in the terminal?
Probably the safest and easy way is to use brew and then just modify your PATH:
First update brew:
brew update
Next install python:
brew install python
That will install and symlink python3 to python, for more details do:
brew info python
Look for the Caveats:
==> Caveats
Python has been installed as
/usr/local/bin/python3
Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
/usr/local/opt/python/libexec/bin
Then add to your path /usr/local/opt/python/libexec/bin:
export PATH=/usr/local/opt/python/libexec/bin:$PATH
The order of the PATH is important, by putting first the /usr/local/opt/python/libexec/bin will help to give preference to the brew install (python3) than the one is in your system located in /usr/bin/python
Before we make the changes, the default version of python in my system was python 2.7.17.
python --version
Python 2.7.17
To make python3 as default python by replacing python2 in Ubuntu.
Open Terminal
cd
nano ~/.bashrc
alias python=python3 (Add this line on top of .bashrc file)
Press ctr+o (To save the file)
Press Enter
Press ctr+x (To exit the file)
source ~/.bashrc OR . ~/.bashrc (To refresh the bashrc file)
python --version
Python 3.7.5
Changing the default python version system wide can break some applications that depend on python2. The alternative solution would be to create an alias.
If you are using zsh (the default on Mac OS) run the following from terminal:
echo 'alias python="python3"' >> ~/.zshrc
echo 'alias pip="pip3"' >> ~/.zshrc
According to this S.O. post, changing the default Python interpreter could possibly break some applications that depend on Python 2.
The post also refers to using aliasing as a solution, and this link might also be a good reference on how to do that.
Personally, I just type "Python3" before I run scripts or go into a shell environment instead of "python".
I have 3 versions of python installed on my Mac. 2 of them are through brew i.e. python2 and python3 while the native version is python. the problem is when I put brew install python it installs python2 and not python. (By saying python means the version which runs on putting that command in terminal). What should I do so that if I type python my brew installed python launches.I have my path variables set correctly and the brew installation path is ahead than that of the usr/bin The problem I am encountering is that I have nltk installed through pip, pip2 and pip3 and when I import nltk in python2 and python3 there is no problem but when I do that in python it show no module found.
Try which python in a terminal to see which python will run. Then you know and can act accordingly to fix it.
ls -lsa $(which python) will let you see if it is a symlink to another location or a real executable. if a Symlink you can see where it points to and so you can follow the breadcrumbs to the final binary used.
if you run python from a terminal and in the python REPL do the following
import sys
print sys.path
you can see to which site-packages locations are pointed.
Other than that you of course have the option to use virtual environments to set up your version of python
if you really want python2 to be the default python command you can add a symlink to your ~/bin folder (create it if it does not exist)
mkdir ~/bin
cd ~/bin
ln -s $(which python2) python
chmod +x python
and make sure that export PATH=~/bin:$PATH is added at the back of your .bashrc or .profile or .zshrc file
Now start a new terminal session and try out python again it should point to brews version
Hope that helps