113

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
Mona Jalal
  • 34,860
  • 64
  • 239
  • 408

22 Answers22

176

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.

Community
  • 1
  • 1
LancDec
  • 2,078
  • 1
  • 12
  • 7
  • 7
    That saved me. FWIW, I had to do three steps to fix my Python on El Capitan: brew install python; brew linkapps python; brew link --overwrite python – Saish Nov 27 '15 at 17:17
  • how to do the reverse of that? – bubakazouba Jun 28 '16 at 00:12
  • 3
    reverse: brew unlink python – Anders B Jan 05 '18 at 10:12
  • 2
    Just to add to this. I found that I had to run "xcode-select --install" for whatever reason. "brew reinstall python" would not work till I did that. In my particular instance, I used migration assistant to move from one physical computer to the other. After those two things, everything was resolved and pip3 worked. – chrisallick Aug 17 '18 at 20:37
  • 3
    Python was already correctly linked for me, what helped was `pip3 install --upgrade pip` as pointed out by @Excalibur in a comment way further down. – retrovertigo May 04 '20 at 07:51
  • I can not reverse this and got totally messed up – Alex Mar 31 '22 at 08:23
  • 1
    @retrovertigo `pip3 install --upgrade pip` also worked for me as well after doing the above solution – that_developer May 29 '22 at 03:34
65

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.

Steven Kryskalla
  • 14,179
  • 2
  • 40
  • 42
  • great explanations. Sidenote : having to debug the package manager of a language installed for getting a project out of mercurial is what makes me loose faith in all those computer thingy. oops.. And now for a new python error "AttributeError local repository" – nicolas Oct 30 '15 at 15:42
  • 1
    I have the same problem, but this didn't work for me, even after following these instructions. PIP is still using an incorrect path. Is there another way to go about this? – Adam Starrh Nov 13 '15 at 19:44
  • 6
    I had to install pip: `sudo /usr/bin/easy_install pip` - Found it here: http://stackoverflow.com/questions/27308234/usr-bin-python-vs-opt-local-bin-python2-7-on-os-x – Adam Starrh Nov 13 '15 at 19:50
  • The problem is that if that's the only version of pip that's installed then you are out of luck – eco Aug 22 '19 at 21:01
57

Only solution in OSX and its variant.

ln -s /usr/local/bin/python /usr/local/opt/python/bin/python2.7
TheExorcist
  • 1,966
  • 1
  • 19
  • 25
  • 1
    Tried to present the answer in a seamless and simple way. – TheExorcist Dec 05 '18 at 09:45
  • 1
    This is likely to have side-effects on other programs using python. Moreover, the end-of-life for Python 2 is January 1st, 2020, and its devs have indicated that eventually (or in some environments, already) "python" with no modifier should refer to Python 3 https://www.python.org/dev/peps/pep-0394/ – kotoole Mar 18 '19 at 20:56
  • 2
    @TheExorcist unfortunately for all of us, Python2 and Python3 are different languages. Code written for one language will break when run in the other. Replacing python with python2.7 wholesale seems highly likely to break other applications down the road. This solution "works" the same way deleting your front door works when you forgot your keys. It gets you through, but don't come crying when your house starts filling with bugs. – kotoole Jul 26 '19 at 15:27
  • @kotoole offcourse, you are right, but sometimes we gotta do what we need to do at the moment. – TheExorcist Jul 27 '19 at 07:32
  • This is a great example of the eternal battle between the Software Developer and the Operations with different perspectives....fun to watch as a DevOps. – eco Aug 22 '19 at 21:12
  • 1
    To avoid the problems discussed here, you can do `which python2.7` and then put the output from that as the first argument, rather than just "python". For me it came out to `ln -s /usr/bin/python2.7 /usr/local/opt/python/bin/python2.7` – jessica Apr 03 '20 at 23:39
46

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.

Community
  • 1
  • 1
kotoole
  • 1,646
  • 1
  • 15
  • 16
  • 3
    Yup, having had Python 2.7 and 3.6 on Mac OSX, then upgrading to 3.6, I had this problem. This was the solution that worked for me (`pip3 install --upgrade pip`) – Excalibur Apr 25 '19 at 03:00
  • Nope: pip Traceback (most recent call last): File "/usr/local/bin/pip", line 7, in from pip._internal import main ImportError: No module named pip._internal – eco Aug 22 '19 at 21:15
  • @Excalibur I added your solution as a comment under the accepted answer. Thanks for that! – retrovertigo May 04 '20 at 07:52
27

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.

m00am
  • 5,910
  • 11
  • 53
  • 69
DaveSaunders
  • 409
  • 4
  • 5
17

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
Simon Briggs
  • 1,249
  • 1
  • 11
  • 14
15

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.
user2771704
  • 5,994
  • 6
  • 37
  • 38
sahar
  • 151
  • 1
  • 2
9

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
Community
  • 1
  • 1
Jordan
  • 390
  • 4
  • 9
9

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)
feuyeux
  • 1,158
  • 1
  • 9
  • 26
5

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.
d g
  • 1,594
  • 13
  • 13
  • This worked. But is it a long term fix? I notice this issue with a couple other things, so this would require changing it for each instance it no longer works, and I'm concerned about the long term effects of this. – Jake T. May 09 '18 at 14:40
  • Definitely not a long term fix. In my case I had mucked around with my python environment which is why I think it got into this situation and looked for a quick fix. I suspect uninstalling virtualenv then reinstalling it with a functional python2.7 pip would make this stick. – d g May 09 '18 at 20:16
4

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

yabrol
  • 41
  • 4
3

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.

2

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
Bussller
  • 1,961
  • 6
  • 36
  • 50
Junior Vieira
  • 553
  • 5
  • 5
1

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.

Simon
  • 19,658
  • 27
  • 149
  • 217
1
sudo /usr/bin/easy_install pip

this command worked out for me

1

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

Hedger
  • 112
  • 1
  • 6
1

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

Nimr Sawafta
  • 594
  • 6
  • 15
1

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.

Wojciech Bednarski
  • 6,033
  • 9
  • 49
  • 73
1

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/python@3.7/bin/pip3)
  3. Check if the executable exists at the referenced location ls /usr/local/opt/python@3.7/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/python@3.7/bin/pip3 /usr/local/bin/pip
Glen Thompson
  • 9,071
  • 4
  • 54
  • 50
1

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

Tanya Gupta
  • 560
  • 4
  • 15
1

The below command works for me in Mac OS 12.3.1:

python3 -m pip install mercurial
Rene
  • 976
  • 1
  • 13
  • 25
Dhanraj
  • 11
  • 1
0

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.

Vaulstein
  • 20,055
  • 8
  • 52
  • 73