python使用pip install时SyntaxError: invalid syntax错误解决办法及模块包的安装、卸载总结

安装模块时遇到的问题,在这里总结,以免长久不用忘记

文章目录

    • pip无法调用
    • 下载失败
    • 在线安装matplotlib包
    • 离线安装cv2包
    • 卸载包
    • 离线升级包
    • 查看已安装的包
    • pip国内的一些镜像
    • 感谢原文

pip无法调用

刚入python,需要用到第三方模块,但是按照教程使用>>>pip install 总是出现错误提示
网上查询许久:语句没错;安装没错;环境配置也正常

最后才知道是不能先进入python模式,而是使用pip直接安装
即在cmd窗口下直接执行,即可正常运行

Microsoft Windows [版本 10.0.18363.1016]
(c) 2019 Microsoft Corporation。保留所有权利。

C:\Users\15778>pip install matplotlib
Collecting matplotlib
  Downloading matplotlib-3.3.1-1-cp37-cp37m-win_amd64.whl (8.9 MB)
     |█████                           | 1.3 MB 3.1 kB/s eta 0:41:12

下载失败

在下载过程又提示

C:\Users\15778>pip install matplotlib
Collecting matplotlib
  Downloading matplotlib-3.3.1-1-cp37-cp37m-win_amd64.whl (8.9 MB)
     |█████                           | 1.3 MB 3.1 kB/s eta 0:41:12ERROR: Exception:
Traceback (most recent call last):
  File "d:\programs\python\python37\lib\site-packages\pip\_vendor\urllib3\response.py", line 425, in _error_catcher
    yield
  File "d:\programs\python\python37\lib\site-packages\pip\_vendor\urllib3\response.py", line 507, in read
    data = self._fp.read(amt) if not fp_closed else b""
  File "d:\programs\python\python37\lib\site-packages\pip\_vendor\cachecontrol\filewrapper.py", line 62, in read
    data = self.__fp.read(amt)
  File "d:\programs\python\python37\lib\http\client.py", line 461, in read
    n = self.readinto(b)
  File "d:\programs\python\python37\lib\http\client.py", line 505, in readinto
    n = self.fp.readinto(b)
  File "d:\programs\python\python37\lib\socket.py", line 589, in readinto
    return self._sock.recv_into(b)
  File "d:\programs\python\python37\lib\ssl.py", line 1071, in recv_into
    return self.read(nbytes, buffer)
  File "d:\programs\python\python37\lib\ssl.py", line 929, in read
    return self._sslobj.read(len, buffer)
    ……
    ……
    WARNING: You are using pip version 20.1.1; however, version 20.2.2 is available.
You should consider upgrading via the 'd:\programs\python\python37\python.exe -m pip install --upgrade pip' command.

最后提示升级pip,升级pip过程中出现超时,第二次尝试成功了。
也可以从国内镜像资源下载。

python -m pip install --upgrade pip -ihttp://pypi.douban.com/simple --trusted-host pypi.douban.com

在线安装matplotlib包

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple matplotlib

会默认安装最新版本的包,如果需要安装指定版本

pip install matplotlib==3.3.1

还遇到一个小插曲,https输成了http,提示安全问题

C:\Users\15778>pip install -i http://pypi.tuna.tsinghua.edu.cn/simple matplotlib
Looking in indexes: http://pypi.tuna.tsinghua.edu.cn/simple
WARNING: The repository located at pypi.tuna.tsinghua.edu.cn is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host pypi.tuna.tsinghua.edu.cn'.
ERROR: Could not find a version that satisfies the requirement matplotlib (from versions: none)
ERROR: No matching distribution found for matplotlib

离线安装cv2包

#cd到.whl文件夹
C:\Users\15778>cd C:\Users\15778\Downloads

C:\Users\15778\Downloads>pip install opencv_python-3.4.9.33-cp38-cp38-win_amd64.whl
ERROR: opencv_python-3.4.9.33-cp38-cp38-win_amd64.whl is not a supported wheel on this platform.

错误提示,原来cp后才是对应python版本号,我的版本3.7.9,下载对应cp37

C:\Users\15778\Downloads>pip install opencv_python-3.4.9.33-cp37-cp37m-win_amd64.whl
Processing c:\users\15778\downloads\opencv_python-3.4.9.33-cp37-cp37m-win_amd64.whl
Requirement already satisfied: numpy>=1.14.5 in d:\programs\python\python37\lib\site-packages (from opencv-python==3.4.9.33) (1.19.1)
Installing collected packages: opencv-python
Successfully installed opencv-python-3.4.9.33

安装完成后进入python环境,

C:\Users\15778\Downloads>python
Python 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> 

未出现提示,表明安装成功
手动安装的包位置在*\Python\Python37\Lib\site-packages下,对应名称和版本的文件夹

卸载包

有时包版本更迭会导致项目出错,需要更换包的版本或单纯为了卸载无用包(虽然占空间并不大),卸载同样适用pip,使用语句>>>pip uninstall *
例如卸载opencv_python

C:\Users\*****>pip uninstall opencv_python

其中需要确认是否继续(y/n),y即可,最后提示successfull说明卸载成功,可以安装其他版本的包了

离线升级包

安装包下好,按路径使用upgrade

C:\Users\lenovo\Downloads>pip install --upgrade scipy-1.5.3-cp37-cp37m-win_amd64.whl
Processing c:\users\lenovo\downloads\scipy-1.5.3-cp37-cp37m-win_amd64.whl
Requirement already satisfied, skipping upgrade: numpy>=1.14.5 in c:\users\lenovo\appdata\local\programs\python\python37\lib\site-packages (from scipy==1.5.3) (1.19.4)
Installing collected packages: scipy
Successfully installed scipy-1.5.3

查看已安装的包

在cmd中运行-pip list或者-pip freeze

查看过时的库

pip list --outdated

python 标准库位置: %python安装路径%\Lib

第三方库位置: %python安装路径%\Lib\site-packages

pip国内的一些镜像

阿里云 http://mirrors.aliyun.com/pypi/simple/
  中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
  豆瓣(douban) http://pypi.douban.com/simple/
  清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
  中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/

感谢原文

yasinzhang的博客

你可能感兴趣的:(python)