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

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

This question is related to python macos installation pip osx-mavericks

The answer is


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

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.


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


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.

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

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


sudo /usr/bin/easy_install pip

this command worked out for me


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.


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)

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).


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

Only solution in OSX and its variant.

ln -s /usr/local/bin/python /usr/local/opt/python/bin/python2.7

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.


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.


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.


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

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.

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.


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to macos

Problems with installation of Google App Engine SDK for php in OS X dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac Could not install packages due to an EnvironmentError: [Errno 13] How do I install Java on Mac OSX allowing version switching? Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) How can I install a previous version of Python 3 in macOS using homebrew? Could not install packages due to a "Environment error :[error 13]: permission denied : 'usr/local/bin/f2py'"

Examples related to installation

You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) Conda version pip install -r requirements.txt --target ./lib How to avoid the "Windows Defender SmartScreen prevented an unrecognized app from starting warning" PackagesNotFoundError: The following packages are not available from current channels: Tensorflow import error: No module named 'tensorflow' Downgrade npm to an older version Create Setup/MSI installer in Visual Studio 2017 how to install tensorflow on anaconda python 3.6 Application Installation Failed in Android Studio How to install pip for Python 3.6 on Ubuntu 16.10?

Examples related to pip

How to fix error "ERROR: Command errored out with exit status 1: python." when trying to install django-heroku using pip "E: Unable to locate package python-pip" on Ubuntu 18.04 How to Install pip for python 3.7 on Ubuntu 18? What is the meaning of "Failed building wheel for X" in pip install? Could not install packages due to an EnvironmentError: [Errno 13] How do I install Python packages in Google's Colab? Conda version pip install -r requirements.txt --target ./lib pip: no module named _internal AttributeError: Module Pip has no attribute 'main' Error after upgrading pip: cannot import name 'main'

Examples related to osx-mavericks

Composer could not find a composer.json pip installation /usr/local/opt/python/bin/python2.7: bad interpreter: No such file or directory Where is my m2 folder on Mac OS X Mavericks How to set JAVA_HOME environment variable on Mac OS X 10.9? dyld: Library not loaded: /usr/local/lib/libpng16.16.dylib with anything php related Xcode is not currently available from the Software Update server Maven not found in Mac OSX mavericks Genymotion, "Unable to load VirtualBox engine." on Mavericks. VBox is setup correctly App can't be opened because it is from an unidentified developer Error executing command 'ant' on Mac OS X 10.9 Mavericks when building for Android with PhoneGap/Cordova