Mac下关于pip和pip3的正确解锁方式

一、pip2的安装

Mac自带python 2.7的环境,可以直接which python查看其安装目录:

~ $ which python
/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python

查看python2.7没问题,则可以用sudo easy_install pip来安装pip2。

~ $ sudo easy_install pip
Password:
Searching for pip
Best match: pip 19.1.1
pip 19.1.1 is already the active version in easy-install.pth
Installing pip script to /usr/local/bin
Installing pip3.7 script to /usr/local/bin
Installing pip3 script to /usr/local/bin

Using /Library/Python/2.7/site-packages
Processing dependencies for pip
Finished processing dependencies for pip

安装完成后,执行pip -Vpip2 -V查看pip版本:

~ $ pip -V
pip 19.2.3 from /Library/Python/2.7/site-packages/pip-19.2.3-py2.7.egg/pip (python 2.7)
~ $ pip2 -V
pip 19.2.3 from /Library/Python/2.7/site-packages/pip-19.2.3-py2.7.egg/pip (python 2.7)

也可以执行which pipwhich pip2查看其安装路径:

~ $ which pip
/usr/local/bin/pip
~ $ which pip2
/usr/local/bin/pip2

之后,只需要运行pip2 install xxx即可安装python2的包,它们将被安装到/Library/Python/2.7/site-packages/目录下

二、pip3的安装

第一步:在终端输入curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

~ $ 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
100 1733k  100 1733k    0     0   191k      0  0:00:09  0:00:09 --:--:--  231k

第二步:执行sudo python3 get-pip.py安装pip3

~ $ sudo python3 get-pip.py
WARNING: The directory '/Users/xulinzhou/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.
WARNING: The directory '/Users/xulinzhou/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pip
  Downloading https://files.pythonhosted.org/packages/30/db/9e38760b32e3e7f40cce46dd5fb107b8c73840df38f0046d8e6514e675a1/pip-19.2.3-py2.py3-none-any.whl (1.4MB)
     |████████████████████████████████| 1.4MB 1.0MB/s 
Installing collected packages: pip
Successfully installed pip-19.2.3

注意:如果执行sudo python get-pip.py,则安装的是pip2。
第三步:执行pip -Vpip3 -V验证pip3的安装

~ $ pip -V
pip 19.2.3 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
~ $ pip3 -V
pip 19.2.3 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)

至此,pip3安装完成。这时候我们惊奇的发现,在安装pip2时,pip等同于pip2,但是在安装完pip3后,pip变成了等同于pip3。所以,以后要安装python2的包时,需要用pip2 install xxx而不是pip install xxx了。

~ $ pip -V
pip 19.2.3 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
~ $ pip2 -V
pip 19.2.3 from /Library/Python/2.7/site-packages/pip (python 2.7)
~ $ pip3 -V
pip 19.2.3 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)

三、如何卸载pip2和pip3

如果只安装了pip2,这时候pip等同于pip2,所以卸载时可以执行sudo pip uninstall pipsudo pip2 uninstall pip

~ $ sudo pip2 uninstall pip
Password:
WARNING: The directory '/Users/xulinzhou/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.
Uninstalling pip-19.2.3:
  Would remove:
    /usr/local/bin/pip
    /usr/local/bin/pip3
    /usr/local/bin/pip3.7
    /usr/local/lib/python3.7/site-packages/pip-19.2.3.dist-info/*
    /usr/local/lib/python3.7/site-packages/pip/*
Proceed (y/n)? y
  Successfully uninstalled pip-19.2.3

卸载pip3的方式和卸载pip2的类似,执行sudo pip3 uninstall pip即可。实际上sudo pip uninstall pip命令则取决于这时候的pip等效于pip2还是pip3,可以通过pip -V查看。题主这里安装完pip3后的pip此时等效的是pip3,未安装pip3之前,pip等效于pip2。

四、如何升级pip2和pip3

执行pip2 install –upgrade pip可升级pip2,如下:

~ $ pip2 install --upgrade pip
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Requirement already up-to-date: pip in /Library/Python/2.7/site-packages (19.2.3)

执行pip3 install –upgrade pip可升级pip3,如下:

~ $ pip3 install --upgrade pip
Requirement already up-to-date: pip in /usr/local/lib/python3.7/site-packages (19.2.3)

同理卸载,升级执行pip install –upgrade pip时,取决于这时候pip等效于pip2还是pip3。

五、切换pip等效成pip2或pip3

如果需要将pip改成等效成pip2,执行sudo vim /usr/local/bin/pip,把第一行的改成:#!/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python即可。如下:

#!/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
# -*- coding: utf-8 -*-
import re
import sys

from pip._internal import main

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

如果需要将pip改成等效成pip3,执行sudo vim /usr/local/bin/pip,把第一行的改成:#!/usr/local/opt/python/bin/python3.7即可。如下:

#!/usr/local/opt/python/bin/python3.7
# -*- coding: utf-8 -*-
import re
import sys

from pip._internal import main

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

这样,可以自由切换pip等效成pip2或pip3。

  • 9
    点赞
  • 19
    收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论 5

打赏作者

三天打鱼,两天晒网

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值