MacOS安装pip失败,提示:SyntaxError: invalid syntax

当尝试使用sudo easy_install命令安装pip时遇到错误,提示语法错误。为解决此问题,可以下载get-pip.py脚本,然后使用python3执行该脚本进行安装。执行成功后,通过pip --version确认pip已成功安装到Python3环境中。注意,以root用户运行pip可能造成权限问题,建议使用虚拟环境。
摘要由CSDN通过智能技术生成

使用命令 easy_install 安装 pip

使用命令 sudo easy_install pip 安装 pip,结果失败了,执行命令的信息如下:

➜  ~ sudo easy_install pip
Password:
Searching for pip
Reading https://pypi.org/simple/pip/
Downloading https://files.pythonhosted.org/packages/da/f6/c83229dcc3635cdeb51874184241a9508ada15d8baa337a41093fab58011/pip-21.3.1.tar.gz#sha256=fd11ba3d0fdb4c07fbc5ecbba0b1b719809420f25038f8ee3cd913d3faa3033a
Best match: pip 21.3.1
Processing pip-21.3.1.tar.gz
Writing /tmp/easy_install-8XXfb1/pip-21.3.1/setup.cfg
Running pip-21.3.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-8XXfb1/pip-21.3.1/egg-dist-tmp-5cqtMF
Traceback (most recent call last):
# 省略100行  
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/setuptools/sandbox.py", line 44, in _execfile
    code = compile(script, filename, 'exec')
  File "/tmp/easy_install-8XXfb1/pip-21.3.1/setup.py", line 7
    def read(rel_path: str) -> str:
                     ^
SyntaxError: invalid syntax

解决方案

下载脚本文件 get-pip.py:

curl 'https://bootstrap.pypa.io/get-pip.py' > get-pip.py

接着使用命令 python3 执行脚本文件 get-pip.py:

sudo python3 get-pip.py

就这样完成安装了。

执行命令的过程信息如下所示:

➜  ~ curl 'https://bootstrap.pypa.io/get-pip.py' > get-pip.py
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 2108k  100 2108k    0     0  2571k      0 --:--:-- --:--:-- --:--:-- 2568k
➜  ~ sudo python3 get-pip.py
Password:
WARNING: The directory '/Users/liaowenxiong/Library/Caches/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you should use sudo's -H flag.
DEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621
Collecting pip
  Downloading pip-21.3.1-py3-none-any.whl (1.7 MB)
     |████████████████████████████████| 1.7 MB 1.0 MB/s
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 21.2.4
    Uninstalling pip-21.2.4:
      Successfully uninstalled pip-21.2.4
DEPRECATION: Configuring installation scheme with distutils config files is deprecated and will no longer work in the near future. If you are using a Homebrew or Linuxbrew Python, please see discussion at https://github.com/Homebrew/homebrew-core/issues/76621
Successfully installed pip-21.3.1
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
➜  ~ pip --version
pip 21.3.1 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)

更简单的执行命令如下:

curl https://bootstrap.pypa.io/get-pip.py | python3

卸载 pip

sudo pip uninstall pip  
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值