国内 pip 镜像源换源方法及 pip 基本操作
pip 是 Python 的包管理工具,允许用户方便地安装、升级和管理 Python 包和库。 是 Python 官方推荐的包管理工具,通常在安装 Python 时默认安装。 本文讲述国内 pip 镜像源换源方法及 pip 基本操作
https://pypi.tuna.tsinghua.edu.cn/simple
https://mirrors.aliyun.com/pypi/simple
https://pypi.douban.com/simple
https://pypi.mirrors.ustc.edu.cn/simple
https://mirrors.cloud.tencent.com/pypi/simple
注:大家根据自身需要选择合适的镜像源,个人推荐中科大的镜像源
使用公式
pip install 【package_name】 -i 【pip源】
注:
【package_name】为为要 pip安装的包
【pip源】可以从国内常用源的地址随便选一个
通过修改配置文件来永久使用某个镜像源:
使用公式:
pip config set global.index-url 【pip源】
通过下面命令查看当前设置的镜像源:
pip config list
删除全局设置的镜像源:
pip config unset global.index-url
删除用户级别设置的镜像源:
pip config unset global.index-url
检查 pip 的版本:
pip --version
更新 pip 本身:
pip install --upgrade pip
查看当前环境中已安装的所有包及其版本信息:
pip list
安装包与卸载包:
注:【package_name】为包的名字
# 安装 pip install 【package_name】 # 卸载 pip uninstall 【package_name】
希望安装特定版本的包,可以使用 == 语法,公式如下:
pip install 【package_name】==【版本号】
例如:安装numpy的1.0.0版本
pip install numpy==1.0.0
遇到下载不稳定时,可以设置超时时间,公式如下
pip install 【package_name】 --timeout 【时间】
例如,安装numpy。将超时时间设置为 10 秒:
pip install numpy --timeout 10
将当前环境的包导出:
pip freeze > requirements.txt
从 requirements.txt 安装指定包:
pip install -r requirements.txt
自定义源时的信任设置,公式如下
pip install 【package_name】 --trusted-host 【镜像源】
比如安装numpy且信任清华源:
pip install numpy --trusted-host pypi.tuna.tsinghua.edu.cn
换源之后下载的包无法匹配其他依赖包:
ERROR: Could not find a version that satisfies the requirement …
网络连接不上:
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘NewConnectionError(’<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7f4248ebf7c0>: Failed to establish a new connection: [Errno 101] 网络不可达’)':…
raise ReadTimeoutError(self._pool, None, “Read timed out.”) pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host=‘files.pythonhosted.org’, port=443): Read timed out.
加载超时:
return self._sslobj.read(len, buffer) socket.timeout: The read operation timed out During handling of the above exception, another exception occurred: Traceback (most recent call last):…
可以尝试到官网手动下载安装: Python Package 官网
本文作者:Dageling003
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!