前言
之前一直苦于一个问题,比如一些包在Win上安装不了,比如 uvloop
但是为了提高效率,代码中必须有这个模块
在运行中可以通过 os 模块判断是否使用, 那依赖文件呢? requirements.txt 要不要有 uvloop
呢?
正文
其实我们可以通过语法来赋予 requirements.txt 一定的灵活性.
比如,我们使用 pip freeze > requirements.txt
时,输出的大致为
每一行为 模块名==版本号
, 这其实就是一种语法, 用来标记安装的模块的版本号, 其实他还有更多功能
详见官方文档
PEP508-doc
语法有
这里抛砖引玉,只写一个例子,就是根据系统OS判断要不要装某个模块
比如 uvloop
, 不支持 Windows
我们可以这样写
这样就是判断当前环境,如果为 Linux
就安装 0.14.0
的 uvloop
模块,如果是其他环境就 pass
需要注意的是, Linux
是表格中写的可选值(表格第6行列出可选Linux/Windows/Java)
请严格区分大小写并不要写错,写错不会报错但是识别不了