项目下生成 requirements.txt
pip freeze > requirements.txt
pip install -r requirements.txt
项目下生成 requirements.txt
pip freeze > /packages/requirements.txt
从网络下载到/packages(有就不需要)
pip download -d /packages -r /packages/requirements.txt
根据已下载的离线包生成requirements.txt
import os
base_dir = r'/packages'
file_list = os.listdir(base_dir)
def text_save(filename, data):
file = open(filename, 'a')
for i in data:
if i == 'requirements.txt':
continue
print(i)
s = str(i).replace('[', '').replace(']', '') # 去除[],这两行按数据不同,可以选择
s = s.replace("'", '').replace(',', '') + '\n' # 去除单引号,逗号,每行末尾追加换行符
file.write(s)
file.close()
print("保存文件成功")
text_save(r'/packages/requirements.txt', file_list)
批量安装离线包
pip install --no-index --find-links=/packages/ -r /packages/requirements.txt
OK.