Python库安装速度慢通常是因为PyPI(Python Package Index)服务器位于国外,导致网络延迟。为了提高安装速度,您可以使用国内的镜像源。以下是几种方法:
临时使用镜像源
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple 库名
永久配置镜像源
Windows:
在`C:\Users\[用户名]\pip`目录下创建`pip.ini`文件,并添加以下内容:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
Linux:
在`~/.pip/pip.conf`文件中添加以下内容:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
使用conda命令 (如果您使用的是Anaconda或Miniconda):conda install -c conda-forge 库名
conda install -c conda-forge 库名
使用pip的`--default-timeout`参数来增加超时时间:
pip --default-timeout=100 install 库名
下载离线包
访问[Python包索引(PyPI)的下载页面](https://www.lfd.uci.edu/~gohlke/pythonlibs/lxml),找到所需的库,下载对应的`.whl`文件。
然后使用pip安装下载的`.whl`文件:
pip install /path/to/downloaded/file.whl
选择以上方法之一,应该可以显著提高Python库的安装速度。需要注意的是,这些方法可能不适用于所有库,特别是那些有特殊依赖或版本要求的库。