Solve the problem of pip3 corresponding to python2 in mac (pip pointing problem)

Solve the problem of pip3 corresponding to python2 in mac (pip pointing problem)

The same can solve the problem of pip corresponding to Python3.


One, the problem

Check the versions of pip and pip3 in mac, the Python version corresponding to pip3 is 2.7

image-20201221215802703

Under normal circumstances, it should be: pip corresponds to Python2, pip3 corresponds to Python3.

Two, solve

Use which to view the absolute path of the command pip.

~ » which pip                
/usr/local/bin/pip

Using easy_install to install pip in mac will cause both pip and pip3 to become pip

~ » sudo easy_install pip                 littlechieh6@bogon
Password:
Searching for pip
Best match: pip 20.2b1
Processing pip-20.2b1-py2.7.egg
pip 20.2b1 is already the active version in easy-install.pth
Installing pip script to /usr/local/bin
Installing pip3.8 script to /usr/local/bin
Installing pip3 script to /usr/local/bin

Using /Library/Python/2.7/site-packages/pip-20.2b1-py2.7.egg
Processing dependencies for pip
Finished processing dependencies for pip
-------------------------------------------------------------
~ » pip3 --version                        littlechieh6@bogon
pip 20.2b1 from /Library/Python/2.7/site-packages/pip-20.2b1-py2.7.egg/pip (python 2.7)
-------------------------------------------------------------
~ » pip --version                         littlechieh6@bogon
pip 20.2b1 from /Library/Python/2.7/site-packages/pip-20.2b1-py2.7.egg/pip (python 2.7)

Or use the get-pip.py script on the bootstrap.pypa.io website to install pip3, which will cause both pip and pip3 to become pip3

~/Desktop » curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:  1 1842k    1 20565    0     0  43294      0  0:00:43 --:--: 50 1842k   50  935k    0     0   626k      0  0:00:02  0:00:100 1842k  100 1842k    0     0   848k      0  0:00:02  0:00:02 --:--:--  848k
-------------------------------------------------------------
~/Desktop » sudo python3 get-pip.py
-------------------------------------------------------------
~/Desktop » pip --version                 littlechieh6@bogon
pip 20.3.3 from /Library/Python/3.8/site-packages/pip (python 3.8)
-------------------------------------------------------------
~/Desktop » pip3 --version                littlechieh6@bogon
pip 20.3.3 from /Library/Python/3.8/site-packages/pip (python 3.8)

View the execution location of Python2 and Python3

~ » which python3                         littlechieh6@bogon
/usr/bin/python3
-------------------------------------------------------------
~ » which python                          littlechieh6@bogon
/usr/bin/python
-------------------------------------------------------------
~ » ls -al /usr/bin/python3               littlechieh6@bogon
-rwxr-xr-x  1 root  wheel  31488 Sep 22 08:29 /usr/bin/python3
-------------------------------------------------------------
~ » ls -al /usr/bin/python                littlechieh6@bogon
lrwxr-xr-x  1 root  wheel  75 Jul  1 08:44 /usr/bin/python -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7

View the execution location of pip

~ » which pip                             littlechieh6@bogon
/usr/local/bin/pip
-------------------------------------------------------------
~ » which pip3                            littlechieh6@bogon
/usr/local/bin/pip3
-------------------------------------------------------------
~ » ls -al /usr/local/bin/pip             littlechieh6@bogon
-rwxr-xr-x  1 root  admin  263 Dec 21 22:56 /usr/local/bin/pip
-------------------------------------------------------------
~ » ls -al /usr/local/bin/pip3            littlechieh6@bogon
-rwxr-xr-x  1 root  admin  263 Dec 21 22:56 /usr/local/bin/pip3

The key point of the original pip and pip3 scripts is to #!represent the path to execute the script. Since both pip and pip3 are executed by python3, use pip -Vandpip3 -V

#!/Applications/Xcode.app/Contents/Developer/usr/bin/python3
# -*- 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())

Use vim /usr/local/bin/pipto point the interpreter to Python2

#!/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
# -*- 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())

Three, the effect

~ » pip -V                                littlechieh6@bogon
pip 20.2b1 from /Library/Python/2.7/site-packages/pip-20.2b1-py2.7.egg/pip (python 2.7)
-------------------------------------------------------------
~ » pip3 -V                               littlechieh6@bogon
pip 20.3.3 from /Library/Python/3.8/site-packages/pip (python 3.8)

Reference tutorial:

  1. https://blog.csdn.net/u014259820/article/details/100580970

Guess you like

Origin blog.csdn.net/qq_43085611/article/details/111502620
Recommended