pip3: bad interpreter: No such file or directory

110,269

Solution 1

You've got a whole slew of different Python installations, plus at least one former Python installation that you deleted.

Situations like this are exactly why running pip or pip3 directly is no longer recommended, in favor of:

python3 -m pip install whatever

This guarantees that you're absolutely positively running the pip that goes with whatever python3 means, while pip3 just means you're running the pip that goes with some Python 3.x, which may be any of the various ones you've installed.

Or, even better, use virtual environments, so you can rely on the fact that python and pip are the commands from the currently-active environment, and not even worry about what they mean system-wide.


But, if you want to know how you got into this mess and how to fix it:

Your python3 command is probably from a Homebrew Python (you can check; ls -l /usr/local/bin/python3 and see if it's a symlink to something in /usr/local/Cellar/python).

Your pip3 command is from a Python 3 that doesn't exist. Most likely, you installed another Python 3, which overwrote the pip3 from the Homebrew Python 3, and then uninstalled it, leaving a broken pip behind.

The simplest thing to do is to just rm /usr/local/bin/pip3. Then, assuming you want your Homebrew Python to be your default for python3 and pip3, redo the brew link python command. If it shows you any warnings or errors, you still have other things to fix. If not, /usr/local/bin/pip3 should now be the Homebrew 3.6 pip, and which pip3 should pick out /usr/local/bin/pip3, and everything is good until the next time you install another Python 3 and overwrite a bunch of stuff.

A better fix would be to pick one way of installing Python—whether Anaconda, Homebrew, python.org installers, or whatever—and use that consistently. Uninstall everything, reinstall the one you actually want, and never touch the others again. (Unfortunately, you will still be stuck with Apple's system Python 2.7, but if you're only using 3.x, that won't matter.)

Solution 2

You can try to change the python version of pip by doing
vim /path/to/pip
Then change the commented line (first line) with the desired version of Python.

Solution 3

I have the same problem.

For me the path of python in venv/bin/pip was wrong.

1. Open pip:

gedit path/to/pip

#!/home/saeed/project-master/venv/bin/python3.9
# -*- coding: utf-8 -*-
import re
import sys

2. Correct the python path in the first line of pip file:

#!/home/saeed/project/venv/bin/python3.9
# -*- coding: utf-8 -*-
import re
import sys

Solution 4

I'll made an addition, maybe it can help to someone.

I have python3.9 installation at this moment, but I get an error message like TS reported:

/home/username/.local/bin/pip: bad interpreter: /usr/bin/python3.6: No such file or directory

I found that files pip3.6 and pip exists in ~/.local/bin direcotry with following shebang (note that explicit Python version specified):

#!/usr/bin/python3.6

But since I have not 3.6 version on my system, calling

$ pip

obviously causes this error. Replacing 3.6 to 3.9 fixes it.

Share:
110,269
floss
Author by

floss

Updated on July 05, 2022

Comments

  • floss
    floss almost 2 years

    I am trying to install dependencies using pip3 command

    current scenario:

    Dev$ which python
    /Users/Dev/anaconda/bin/python
    
    Dev$ which python3
    /usr/local/bin/python3
    
    
    Dev$ pip --version
    pip 10.0.1 from /usr/local/lib/python2.7/site-packages/pip (python 2.7)
    
    Dev$ pip3 --version
    -bash: /usr/local/bin/pip3: /usr/local/opt/python3/bin/python3.6: bad 
    interpreter: No such file or directory
    

    I have no idea why my pip3 command is not working.

    I have tried things like this:

    brew link --overwrite python 
    
  • Nic Scozzaro
    Nic Scozzaro almost 5 years
    "Or, even better, use virtual environments" ... I was having this problem even while using a virtual environment, but your solution fixed the issue
  • thanatoz
    thanatoz over 4 years
    This actually works and all you have to do is change the python3 version if it points incorrectly there itself.
  • PatrickT
    PatrickT about 4 years
    Good answer. As far as I can see, Xcode Command Line Tools provide python3 as well.
  • Cedric Martens
    Cedric Martens about 4 years
    I get a segmentation fault when I run python -m pip install pandas
  • ibilgen
    ibilgen over 3 years
    Excellent! This worked for me too. I opened the pip and pip3 files with vim, somehow the previous deleted python location was there, I changed it to the correct path and it worked! Thanks.
  • Dawid Loranc
    Dawid Loranc over 3 years
    Worked for me, I had the wrong path to Python from XCode and this tip solved my problem. To list areas where you can find pip you can use 'echo $PATH' command.
  • user14717
    user14717 almost 3 years
    Is there a PEP referencing the "using pip directly is no longer recommended"?
  • mosemos
    mosemos almost 3 years
    Thank you! This solved my problem. I was wondering why it stopped working all of a sudden. Turned out I changed the directory name of the project and the pip file inside the virtualenv had a hard-coded path.
  • Timo
    Timo over 2 years
    You can change the line python3 -m pip .. to a more detailed python3 version such as python3.10 -m pip..
  • Timo
    Timo over 2 years
    This seems a hack as compared to the answer with most votes with brew link python, here is more info to the brew cmd.
  • Chris
    Chris over 2 years
    Please read How to Answer. This answer would be a lot more useful with more context. And note that this question is about macOS, not a Debian derivative.
  • David Gard
    David Gard over 2 years
    @Timo, hack or not, this is the only thing that worked for me. As the accepted answer says thought, python -m pip install whatever is the way to go and I will be using that going forward.
  • Stefan_EOX
    Stefan_EOX over 2 years
    @user14717 I found a blog post directly from Brett Canon: snarky.ca/why-you-should-use-python-m-pip