作者: | 来源:互联网 | 2023-10-11 11:35
文章目录 解决mac中pip3对应python2(pip指向问题) 一、问题 二、解决 三、效果
解决mac中pip3对应python2(pip指向问题) 同理可以解决pip对应Python3的问题。
一、问题 在mac中查看pip和pip3的版本,其中pip3对应的Python版本是2.7
正常情况下应该是:pip对应Python2,pip3对应Python3。
二、解决 使用which可以查看指令pip的绝对路径。
~ » which pip /usr/local/bin/pip
在mac中使用easy_install安装pip会导致pip和pip3都变成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/binUsing /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)
或者使用bootstrap.pypa.io网站的get-pip.py脚本安装pip3,会导致pip和pip3都变成pip3
~/Desktop » curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed0 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)
查看Python2和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
查看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
原pip和pip3脚本,关键点在于#!
代表执行脚本的路径。由于pip和pip3都是由python3执行,所以使用pip -V
和pip3 -V
import reimport sysfrom pip. _internal. cli. main import mainif __name__ == '__main__' : sys. argv[ 0 ] = re. sub( r'(-script\.pyw|\.exe)?$' , '' , sys. argv[ 0 ] ) sys. exit( main( ) )
使用vim /usr/local/bin/pip
将解释器指向Python2
import reimport sysfrom pip. _internal. cli. main import mainif __name__ == '__main__' : sys. argv[ 0 ] = re. sub( r'(-script\.pyw|\.exe)?$' , '' , sys. argv[ 0 ] ) sys. exit( main( ) )
三、效果 ~ » 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)
参考教程:
https://blog.csdn.net/u014259820/article/details/100580970