pip更换镜像源、添加到环境变量、常用功能、常见问题汇总


一、官方地址和国内镜像源地址

下载python

官方地址:https://www.python.org/downloads/
第三方地址(华为镜像源):https://mirrors.huaweicloud.com/python/

下载python第三方库

官方地址:https://pypi.org/
镜像源地址(以下4个目前可用):

  1. 清华:https://pypi.tuna.tsinghua.edu.cn/simple
  2. 阿里:https://mirrors.aliyun.com/pypi/simple
  3. 豆瓣:https://pypi.doubanio.com/simple
  4. 华为:https://mirrors.huaweicloud.com/repository/pypi/simple

pip更换下载地址为国内镜像源地址

临时使用:

pip install -i 镜像源地址  库名

# 临时用阿里云地址
pip install -i https://mirrors.aliyun.com/pypi/simple package_name

永久更改

# 永久改为阿里镜像源地址
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple

二、pip 的常见用法

1.安装第三方库 & 安装指定版本的库

pip install 库名

# 例如:安装 numpy库
pip install numpy

# 指定版本安装:安装2.1.0版本的 numpy库
pip install numpy==2.1.0

2.查看python第三方库的安装路径和版本。

# 查看numpy的安装路径
pip show numpy

在这里插入图片描述

3.卸载第三方包

pip uninstall 包名

在这里插入图片描述
卸载之前会确认一次,输入y或n。

4.更新pip版本 & 更新python库

在更新之前最好确保已经修改了pip下载地址为国内镜像源地址

# 更新 pip
python -m pip install --upgrade pip

# 更新python库
pip list # 列出所有安装的库
pip list --outdated # 列出可以更新的库
pip install --upgrade 库名 # 升级某个库

5.其他用法& pip help

Usage:
  pip <command> [options]
用法: pip+命令+(选项)

Commands(命令):
  download                    Download packages.下载包文件
  freeze                      Output installed packages in requirements format.以要求的格式输出已安装的包。
  list                        List installed packages.列出所有已安装的包
  show                        Show information about installed packages.显示某个已安装包的信息。
  
  check                       Verify installed packages have compatible dependencies.
  							  检验已安装的包的依赖关系是否正常。比如matplotlib依赖numpy库,如果numpy没安装的话就会检查出来
  							  
  config                      Manage local and global configuration.管理本地和全局配置。
  search                      Search PyPI for packages.搜索某个包
  cache                       Inspect and manage pip's wheel cache.检查和管理pip下载的wheel缓存。
  wheel                       Build wheels from your requirements.根据您的需求构建轮子。
  hash                        Compute hashes of package archives.计算包的hash。
  completion                  A helper command used for command completion.
  debug                       Show information useful for debugging.显示对调试有用的信息
  help                        Show help for commands.

General Options(一般选项):
  -h, --help                  Show help.
  --isolated                  Run pip in an isolated mode, ignoring environment variables and user configuration.
  							  以隔离模式运行pip,忽略环境变量和用户配置。
  								
  -v, --verbose               Give more output. Option is additive, and can be used up to 3 times.
  							  加上这个参数可以输出更详细的信息.
  							  
  -V, --version               Show version and exit.显示版本
  
  -q, --quiet                 Give less output. Option is additive, and can be used up to 3 times (corresponding to
                              WARNING, ERROR, and CRITICAL logging levels).
                              加上这个参数可以输出更少的信息.(仅输出警告、错误等关键信息)
                              
  --log <path>                Path to a verbose appending log. 将日志信息添加到指定路径下的文件末尾
  --no-input                  Disable prompting for input.禁用输入提示。
  --proxy <proxy>             Specify a proxy in the form [user:passwd@]proxy.server:port.[user:passwd@]proxy.server:port的形式指定代理。
  						      
  --retries <retries>         Maximum number of retries each connection should attempt (default 5 times).
  							  每个连接应该尝试的最大重试次数(默认为5)--timeout <sec>             Set the socket timeout (default 123.0 seconds).设置套接字连接超时时间
  
  --exists-action <action>    Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup,
                              (a)bort.
  --trusted-host <hostname>   Mark this host or host:port pair as trusted, even though it does not have valid or any
                              HTTPS.将此主机或(主机:端口)标记为受信任,即使它是无效的链接或任何HTTPS。
                              
  --cert <path>               Path to alternate CA bundle.备用CA包的路径。
  --client-cert <path>        Path to SSL client certificate, a single file containing the private key and the
                              certificate in PEM format.SSL客户端证书的路径,包含私钥和PEM格式的证书的单个文件。
  --cache-dir <dir>           Store the cache data in <dir>.将缓存数据存储在<dir>--no-cache-dir              Disable the cache.禁用缓存。
  --disable-pip-version-check
                              Don't periodically check PyPI to determine whether a new version of pip is available for
                              download. Implied with --no-index.不要定期检查是否有新版本的pip可供下载
                              
  --no-color                  Suppress colored output.
  
  --no-python-version-warning
                              Silence deprecation warnings for upcoming unsupported Pythons.
                              对可能出现的不支持本机python的包不进行警告。
                              
  --use-feature <feature>     Enable new functionality, that may be backward incompatible.启用向后不兼容的功能。
  --use-deprecated <feature>  Enable deprecated functionality, that will be removed in the future.启用可能在将来不再支持的功能。

上面help的内容翻译并非十分准确,只具有一定的参考性。


三、pip常见问题

1.‘pip’不是内部或外部命令,也不是可运行的程序或批处理文件。

情况(一)

在安装python的时候没有把python地址添加到系统环境变量中,如下图所示,如果没有勾上下面那个选项,在系统变量中就找不到pip工具:
在这里插入图片描述

解决方法:
找到自己的python安装路径,在该路径下找到Scripts文件夹,进入这个文件夹,(可以看到pip.exe就在这个文件夹内),把这个文件夹的路径添加到系统环境变量中。比如我的python安装在D盘下,所以Scripts文件夹的路径就是D:\python\Scripts

**
**
如何添加环境变量?

进入控制面板,找到查看高级系统设置,然后照下图顺序操作,把Scripts文件夹的路径新建到path里面即可
在这里插入图片描述
上面的界面是win10系统的界面,win7系统操作类似,不过要注意在添加环境变量的时候两个路径之间要用英文的分号 ; 分隔开。

情况(二)

参考网上的一些说法:
在cmd终端执行以下两条命令:

python -m ensurepip
python -m pip install --upgrade pip

参考:

https://blog.csdn.net/haihonga/article/details/100168691
https://www.jb51.net/article/141852.htm

2.pip install XXX.whl文件出现is not a supported wheel on this platform

  • 要确保包的版本和python版本一致,例如python版本是3.6,就只能安装**-cp36**版本的包
  • 要确保包是否符合你的电脑系统,分Windows、Linux、Mac,每个系统下还分有32位和64位
  • pip版本太低,更新pip就可以解决

3.ConnectTimeoutError 超时问题

  • 如果没有修改pip从国内镜像源下载的话,是很容易超时的,所以先考虑修改镜像源地址。

  • 修改超时时长:

    pip config set global.timeout 123
    # 把 timeout时长修改为 123秒
    # 注意global后面还有个点
    
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值