You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取pip下载Python包(.whl)的URL?支持requirements.txt且无需安装

获取pip下载包的URL列表方案

1. 用pip官方的--report参数(最便捷)

pip 21.0及以后版本自带这个功能,运行时会生成一个JSON报告文件,里面直接列了每个包的下载URL,不用自己瞎解析:

  • 跑这个命令:
    pip download -r requirements.txt --report download_report.json
    
  • 打开生成的download_report.json,每个包条目里的url字段就是你要的下载链接。要是不想真的下载文件,加个--dry-run参数试试(不过有些场景下dry-run可能拿不到完整URL,看你需求选)。

2. 解析pip的详细输出

如果你的pip版本比较老,用--verbose让它输出详细日志,然后从日志里抠URL:

  • 先把输出存到文件:
    pip download -r requirements.txt --verbose > download_log.txt 2>&1
    
  • 写个简单的Python脚本提取URL,比如:
    import re
    
    url_regex = re.compile(r'https?://[^\s]+')
    with open('download_log.txt', 'r') as log_file:
        for line in log_file:
            if 'Downloading from URL' in line:
                url_match = url_regex.search(line)
                if url_match:
                    print(url_match.group())
    

3. 调用pip内部API(编程进阶)

要是想直接在Python代码里搞,可以调用pip的内部模块,但要注意:内部API可能随pip版本变,不如前两种靠谱:

from pip._internal.commands.download import DownloadCommand
from pip._internal.cli.main_parser import parse_command

# 解析命令参数
cmd_args = parse_command(['download', '-r', 'requirements.txt', '--verbose'])
download_cmd = DownloadCommand(name='download', summary='')
options, remaining_args = download_cmd.parse_args(cmd_args[1:])
# 后续可以通过download_cmd的内部逻辑获取URL,建议优先用前两种方法

内容的提问来源于stack exchange,提问作者Jack

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 21:15:19