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

如何在PowerShell中使用参数展开(Splatting)批量安装Python项目依赖

用PowerShell参数展开(Splatting)批量安装Python依赖包

要实现批量安装所有requirements*开头文件里的依赖,你可以按以下步骤用PowerShell的参数展开功能:

  1. 获取目标依赖文件
    先筛选出所有符合命名规则的文件(仅取文件,排除目录):

    Get-ChildItem -Filter "requirements*" -File
    
  2. 构建pip的参数数组
    因为pip install需要重复使用-r <文件名>的参数格式,我们需要把每个文件对应转换成'-r'和文件名的组合,存入数组:

    $pipInstallArgs = @()
    Get-ChildItem -Filter "requirements*" -File | ForEach-Object {
        $pipInstallArgs += '-r', $_.FullName
    }
    

    或者用更简洁的一行写法:

    $pipInstallArgs = Get-ChildItem -Filter "requirements*" -File | ForEach-Object { '-r', $_.FullName }
    
  3. 用参数展开执行安装
    利用PowerShell的参数展开(Splatting)语法@,把数组里的元素作为独立参数传给pip install:

    pip install @pipInstallArgs
    

这样就等价于手动执行pip install -r requirements1.txt -r requirements2.txt ...的效果,自动遍历所有匹配的依赖文件完成安装。

内容的提问来源于stack exchange,提问作者Joaquín

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 19:15:34