如何用单条pip install命令同时从requirements.txt及本地安装私有项目?
解决方案
1. 单条命令直接合并安装
直接在同一条pip install命令里同时指定requirements.txt和本地包路径,pip会一次性处理所有安装请求,而且共享同一个私有仓库索引配置:
python3.8 -m pip install -r requirements.txt ./path/to/local/dependency --index-url https://private/repo/dependency/simple
注意点:
- 如果
requirements.txt里已经有这个私有包的远程仓库条目,只要本地包的版本符合requirements里的版本约束,pip会优先装本地的。 - 要是想强制覆盖已装的远程版本,加个
--force-reinstall参数就行:python3.8 -m pip install -r requirements.txt ./path/to/local/dependency --index-url https://private/repo/dependency/simple --force-reinstall
2. 修改requirements.txt集成本地包
如果想让命令更简洁,直接在requirements.txt里加本地包的路径条目,之后一条命令就能搞定所有安装:
在requirements.txt最后加一行:
./path/to/local/dependency
(要是开发时需要实时修改包代码生效,改成-e ./path/to/local/dependency用可编辑模式)
然后执行:
python3.8 -m pip install -r requirements.txt --index-url https://private/repo/dependency/simple
团队协作小技巧:
- 共享的
requirements.txt里可以把本地包条目注释掉,让开发者自己根据本地路径取消注释:# 开发时启用本地包路径 # ./path/to/local/dependency - 用项目根目录的相对路径,只要大家项目结构一致,就不用各自改路径。
3. 搞定令牌管理的麻烦
不想在命令行里直接带包含令牌的索引URL?把私有仓库配置写到pip的配置文件里就行,一次配置终身受用:
配置步骤:
按系统创建对应的pip配置文件:
- Linux/macOS:
~/.config/pip/pip.conf - Windows:
%APPDATA%\pip\pip.ini
- Linux/macOS:
把带令牌的私有仓库索引写进去:
[global] index-url = https://<你的令牌>@private/repo/dependency/simple之后安装命令就不用加
--index-url了,直接跑:python3.8 -m pip install -r requirements.txt ./path/to/local/dependency
这样每个开发者只需要配置一次自己的pip文件,不用每次命令里输令牌,安全又省事。
内容的提问来源于stack exchange,提问作者Ryan Pierce Williams
相关产品推荐
相关产品推荐

