在 Python 中,你可以通过修改 pip 的配置文件或直接在命令行中使用 --index-url 参数来更改镜像源。以下是几种常见的方法:

1. 临时使用镜像源

你可以在安装包时通过 --index-url 参数临时指定镜像源。例如,使用清华大学的镜像源:

pip install 包名 --index-url https://pypi.tuna.tsinghua.edu.cn/simple

2. 永久修改镜像源

你可以通过修改 pip 的配置文件来永久更改镜像源。

方法一:使用命令行设置

你可以使用以下命令来设置默认的镜像源:

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
方法二:手动修改配置文件

你可以手动编辑 pip 的配置文件。配置文件的路径通常位于:

  • Linux/macOS: ~/.pip/pip.conf
  • Windows: %APPDATA%\pip\pip.ini

在配置文件中添加以下内容:

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

3. 常用镜像源列表

以下是一些常用的 PyPI 镜像源:

  • 清华大学: 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

4. 恢复默认源

如果你想恢复默认的 PyPI 源,可以删除配置文件中的 index-url 配置,或者使用以下命令:

pip config unset global.index-url

这样,pip 就会重新使用默认的 https://pypi.org/simple 源。

5. 使用 requirements.txt 文件

如果你在 requirements.txt 文件中指定了依赖包,可以在安装时使用 --index-url 参数:

pip install -r requirements.txt --index-url https://pypi.tuna.tsinghua.edu.cn/simple

通过以上方法,你可以轻松地修改 Python 的镜像源,以加快包的下载速度。

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐