pip installation /usr/local/opt/python/bin/python2.7: bad interpreter: No such file or directory

Question:

I don’t know what’s the deal but I am stuck following some stackoverflow solutions which gets nowhere. Can you please help me on this?

  Monas-MacBook-Pro:CS764 mona$ sudo python get-pip.py
    The directory '/Users/mona/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
    The directory '/Users/mona/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
    /tmp/tmpbSjX8k/pip.zip/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
    Collecting pip
      Downloading pip-7.1.0-py2.py3-none-any.whl (1.1MB)
        100% |████████████████████████████████| 1.1MB 181kB/s 
    Installing collected packages: pip
      Found existing installation: pip 1.4.1
        Uninstalling pip-1.4.1:
          Successfully uninstalled pip-1.4.1
    Successfully installed pip-7.1.0
    Monas-MacBook-Pro:CS764 mona$ pip --version
    -bash: /usr/local/bin/pip: /usr/local/opt/python/bin/python2.7: bad interpreter: No such file or directory
Asked By: Mona Jalal

||

Answers:

I’m guessing you have two python installs, or two pip installs, one of which has been partially removed.

Why do you use sudo? Ideally you should be able to install and run everything from your user account instead of using root. If you mix root and your local account together you are more likely to run into permissions issues (e.g. see the warning it gives about “parent directory is not owned by the current user”).

What do you get if you run this?

$ head -n1 /usr/local/bin/pip

This will show you which python binary pip is trying to use. If it’s pointing /usr/local/opt/python/bin/python2.7, then try running this:

$ ls -al /usr/local/opt/python/bin/python2.7

If this says “No such file or directory”, then pip is trying to use a python binary that has been removed.

Next, try this:

$ which python
$ which python2.7

To see the path of the python binary that’s actually working.

Since it looks like pip was successfully installed somewhere, it could be that /usr/local/bin/pip is part of an older installation of pip that’s higher up on the PATH. To test that, you may try moving the non-functioning pip binary out of the way like this (might require sudo):

$ mv /usr/local/bin/pip /usr/local/bin/pip.old

Then try running your pip --version command again. Hopefully it picks up the correct version and runs successfully.

Answered By: Steven Kryskalla

I had similar issue. Basically pip was looking in a wrong path (old installation path) or python. The following solution worked for me:

  • I checked where the python path is (try which python)
  • I checked the first line on the pip file (/usr/local/bin/pip2.7 and /usr/local/bin/pip). The line should state the correct path to the python path. In my case, didn’t. I corrected it and now it works fine.
Answered By: sahar

I had used home-brew to install 2.7 on OS X 10.10 and the new install was missing the sym links. I ran

brew link --overwrite python

as mentioned in How to symlink python in Homebrew? and it solved the problem.

Answered By: LancDec

I made the same error using sudo for my installation. (oops)

brew install python
brew linkapps python
brew link --overwrite python 

This brought everything back to normal.

Answered By: DaveSaunders

In case it helps anyone, the solution mentioned in this other question worked for me when pip stopped working today after upgrading it: Pip broken after upgrading

It seems that it’s an issue when a previously cached location changes, so you can refresh the cache with this command:

hash -r
Answered By: Jordan

In my case, I decided to remove the homebrew python installation from my mac as I already had two other versions of python installed on my mac through MacPorts. This caused the error message.

Reinstalling python through brew solved my issue.

Answered By: Simon

I had the same issue. I have both Python 2.7 & 3.6 installed. Python 2.7 had virtualenv working, but after installing Python3, virtualenv kept looking for version 2.7 and couldn’t find it.
Doing pip install virtualenv installed the Python3 version of virtualenv.

Then, for each command, if I want to use Python2, I would use virtualenv --python=python2.7 somecommand

Answered By: yabrol

To simplify to operation, we can use the below command to reinstall version 2:


brew install python@2

Then on my mac, it looks as below:

▶ python -V
Python 2.7.10

▶ python2 -V
Python 2.7.14

▶ python3 -V
Python 3.6.5

▶ pip2 -V
pip 9.0.3 from /usr/local/lib/python2.7/site-packages (python 2.7)

▶ pip3 -V
pip 9.0.3 from /usr/local/lib/python3.6/site-packages (python 3.6)

▶ pip --version
pip 9.0.3 from /usr/local/lib/python2.7/site-packages (python 2.7)
Answered By: feuyeux
sudo /usr/bin/easy_install pip

this command worked out for me

Answered By: user9022502

I got same problem. If I run brew link --overwrite python2. There was still zsh: /usr/local/bin//fab: bad interpreter: /usr/local/opt/python/bin/python2.7: no such file or directory.

cd /usr/local/opt/
mv python2 python

Solved it! Now we can use python2 version fabric.

=== 2018/07/25 updated

There is convinient way to use python2 version fab when your os python linked to python3. .sh for your command.

# fab python2
cd /usr/local/opt
rm python
ln -s python2 python

# use the fab cli
...

# link to python3
cd /usr/local/opt
rm python
ln -s python3 python

Hope this helps.

Answered By: Peter. Chen

I had the same issue, virtualenv was pointing to an old python path. Fixing the path resolved the issue:

$ virtualenv -p python2.7 env
-bash: /usr/local/bin/virtualenv: /usr/local/opt/python/bin/python2.7: bad interpreter: No such file or directory

$ which python2.7
/opt/local/bin/python2.7

# needed to change to correct python path
$ head  /usr/local/bin/virtualenv
#!/usr/local/opt/python/bin/python2.7 <<<< REMOVED THIS LINE
#!/opt/local/bin/python2.7 <<<<< REPLACED WITH CORRECT PATH

# now it works:
$ virtualenv -p python2.7 env
Running virtualenv with interpreter /opt/local/bin/python2.7
New python executable in env/bin/python
Installing setuptools, pip...done.
Answered By: d g

Only solution in OSX and its variant.

ln -s /usr/local/bin/python /usr/local/opt/python/bin/python2.7
Answered By: TheExorcist

Fixing pip

For this error:

~/Library/Python/2.7/bin/pip: /usr/local/opt/python/bin/python2.7: bad interpreter: No such file or directory`

The source of this problem is a bad python path hardcoded in pip (which means it won’t be fixed by e.g. changing your $PATH). That path is no longer hardcoded in the lastest version of pip, so a solution which should work is:

pip install --upgrade pip

But of course, this command uses pip, so it fails with the same error.

The way to bootstrap yourself out of this mess:

  1. Run which pip
  2. Open that file in a text editor
  3. Change the first line from #!/usr/local/opt/python/bin/python2.7 to e.g. #!/usr/local/opt/python2/bin/python2.7 (note the python2 in the path), or any path to a working python interpreter on your machine.
  4. Now, pip install --upgrade pip (this overwrites your hack and gets pip working at the latest version, where the interpreter issue should be fixed)

Fixing virtualenv

For me, I found this issue by first having the identical issue from virtualenv:

~/Library/Python/2.7/bin/virtualenv: /usr/local/opt/python/bin/python2.7: bad interpreter: No such file or directory`

The solution here is to run

pip uninstall virtualenv
pip install virtualenv

If running that command gives the same error from pip, see above.

Answered By: kotoole

Because I had both python 2 and 3 installed on Mac OSX I was having all sorts of errors.

I used which to find the location of my python2.7 file (/usr/local/bin/python2.7)

which python2.7

Then I symlinked my real python2.7 install location with the one the script expected:

ln -s /usr/local/bin/python2.7 /usr/local/opt/python/bin/python2.7
Answered By: Simon Briggs

Editing the first line of this file worked to me:

MBP-de-Jose:~ josejunior$ which python3

/usr/local/Cellar/python/3.7.3/bin/python3

MBP-de-Jose:~ josejunior$

before

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

after

#!/usr/local/Cellar/python/3.7.3/bin/python3
Answered By: Junior Vieira

All you need to do is… close the terminal window and reopen new one to fix this issue.

The issue is, new python path is not added to bashrc(Either source or new terminal window would help).

Answered By: Hedger

For me, on centOS 7
I had to remove the old pip link from /bin by

rm /bin/pip2.7 
rm /bin/pip

then relink it with

sudo ln -s  /usr/local/bin/pip2.7 /bin/pip2.7

Then if

/usr/local/bin/pip2.7

Works, this should work

Answered By: Nimr Sawafta

You could have two different versions of Python and pip.

Try to:

pip2 install --upgrade pip and then pip2 install -r requirements.txt

Or pip3 if you are on newer Python version.

Answered By: Wojciech Bednarski

TLDR: pip found in your path a is a symlink and the referenced location no longer contains the executable. You need to update the symlink.

It helps to understand a couple of things.

  • When you type something like python or pip you os will search /etc/paths to try to find the associated executable for that command. You can see everything in there by using cat /etc/paths.
  • To determine the location of the executable that your shell will use there is a handy command which, you can type which python or which pip. This will tell you the location of the executable that your shell will use for that command.
  • This part is key. The location may or may not be an actual executable, it could be a symbolic link (symlink).
  • Its common for /etc/paths to contain /usr/local/bin, its also common for /usr/local/bin to be a bunch of symlinks to the actual executables. Not the executables themselves.
  • If the executable at the symlinks referenced location doesn’t exist you will get an error like bad interpreter: No such file or directory

With that being said the problem is likely that pip is a symlink and the linked executable probably doesn’t exist at that location anymore. To fix it do the following

  1. Find the location of the executable – which pip (gives something like this /usr/local/bin/pip)
  2. Check the symlink reference location ls -l /usr/local/bin/pip | grep pip (give something like this pip -> /usr/local/opt/[email protected]/bin/pip3)
  3. Check if the executable exists at the referenced location ls /usr/local/opt/[email protected]/bin/pip3 (you are having this issue so it probably doesn’t).
  4. Remove the old symlink rm -r /usr/local/bin/pip
  5. Find the actual pip executable if using homebrew it will be in /usr/local/opt you can use something like ls /usr/local/opt/ | grep python to find it.
  6. Add the right symlink for the pip executable.
    ln -s /usr/local/opt/[email protected]/bin/pip3 /usr/local/bin/pip
Answered By: Glen Thompson

This error typically pops up every time there is an iOS upgrade. Try

xcode-select --install

This will install the latest xcode version and that should fix it

Answered By: Tanya Gupta

The below command works for me in Mac OS 12.3.1:

python3 -m pip install mercurial
Answered By: Dhanraj

I was getting the error while running aws cli.

Error:

zsh: /Users/XX/Library/Python/3.7/bin/aws: bad interpreter: /usr/local/opt/python/bin/python3.7: no such file or directory

Reload your zshrc profile:

source ~/.zshrc

aws cli was installed with a previous version of Python, which had been updated to python3.9.

Running below commands fixed it for me:

brew uninstall awscli

brew install awscli

Uninstall the package causing the issue and reinstall.
The warning remains in some cases but the command will work perfectly fine.

Answered By: Vaulstein