Madhav Thaker
Madhav Thaker

Reputation: 370

python 2.7 shows up when running where pip3

As the title states, I'm seeing an incorrect version of python showing when I check pip3 --version

pip 20.2.4 from /Library/Python/2.7/site-packages/pip-20.2.4-py2.7.egg/pip (python 2.7)

A few other checks:

  1. which pip3: /usr/local/bin/pip3
  2. which python3: /usr/bin/python3

I'm not sure how this happened so some insight here would be great. This is causing issues as I'm not installing any packages with python 3.

Upvotes: 0

Views: 149

Answers (2)

KetZoomer
KetZoomer

Reputation: 2915

If you do have the brew package manager, you can do brew install python

If you don't, you can run /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" to install it, then do the above step.

What this does, is the following,

python -> Python 3
python3 -> Python 3
python2 -> Python 2
pip -> Pip 3
pip2 -> Pip 2
pip3 -> Pip 3

This way, python point to python3, but you also still have access to python 2 (by using the command python2)

Upvotes: 0

OneRaynyDay
OneRaynyDay

Reputation: 3978

You have two ways to fix this that I know of. I ran into this as well:

  1. Use python3 -m pip install ... and forget about it
  2. Actually understand what's going on - pip is actually a scipt that gets invoked by a python executable defined in its shebang:
❯ cat $(which pip)
#!/Users/me/anaconda/bin/python

# -*- coding: utf-8 -*-
import re
import sys

from pip._internal.cli.main import main

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    sys.exit(main())

Let's do an experiment. This pip corresponds to my python3.8 installation. I'll create a python3.6 version for the sake of demonstration.

❯ conda create -n p3.6 python=3.6 anaconda

Then we change the shebang to the new 3.6 installation:

#!/Users/me/anaconda/envs/p3.6/bin/python

And run pip --version on the pip that belongs to my python 3.8 installation:

❯ pip --version
pip 20.1.1 from /Users/rzhang/anaconda/envs/p3.6/lib/python3.6/site-packages/pip (python 3.6)

As you can see it shows that it points to this python3.6 python instead.

The reason this is the case is because if you look at the sys.argv[0] = ... line, it adopts the current python used to invoke the script. Modifying that before going into main() "picks" the python selected from shebang. Try changing that to the realpath of your python installation.

Now if you really want to fix the problem by understanding what's really going on, chances are your python interpreter links are all screwed up from a weird sequence of installations. It may be that the file in your shebang is actually a symlink that is not actually pointing at the correct installation, and you should fix that on a per-situation basis. There is no easy solution other than to untangle the installation paths of your multiple python interpreters.

Hope this helps!

Upvotes: 1

Related Questions