前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >win10使用脚本批量下载本机python依赖包

win10使用脚本批量下载本机python依赖包

作者头像
静谧星空TEL
发布2021-04-27 14:43:25
6960
发布2021-04-27 14:43:25
举报

问题描述

很多公司的生成环境都需要离线安装 如何快速将windows下的python依赖包下载到本地呢?

问题解决

将本机的python依赖包写入txt 使用python脚本处理txt依赖包格式 使用bat脚本批量读取并下载依赖包

详细步骤

1、将本机依赖导入txt

代码语言:javascript
复制
pip list > denpend.txt
代码语言:javascript
复制
cat depend.txt

2、去除依赖版本

忽略版本:打开denpend.txt把版本号和空格去掉 指定版本:使用python脚本

replace.py

代码语言:javascript
复制
#-*- encoding: utf-8 -*-

def isonesplace(str):
    """判断字符串是否只有一个空格"""
    str = str.split(" ")
    return len(str)

def func(str):
    """将中间的空格替换成双等号=="""
    str = str.strip()
    while("==" not in str and isonesplace(str) != 2):
        str = str.replace("  ", " ")
    return str.replace(" ", "==")

def readfile(file):
    """返回file文件去除前两行和全部空格的字符串"""
    ## 读取depend.txt到list
    fr = open(file, 'r', encoding='utf-8')
    list = fr.readlines()
    fr.close()
    comment = ""
    num = 0
    ## 去除前两行和空格,将读取内容存入变量
    for i in list:
        num += 1
        if num > 2:
            row = i.strip()
            comment += row + "\n"
    # print(comment)
    return comment

def writefile(file, str):
    """将str字符串写入file文件"""
    comment = ""
    str = str.split("\n")
    num = 1
    for row in str:
        num += 1
        if (row is ""):
            break
        if(num < len(str)):
            comment += func(row) + "\n"
        else:
            comment += func(row)
    print(comment)
    # # 清空depend.txt
    # open(file, 'w').close()
    # 将comment重新写入depend.txt
    fw = open(file, 'w')
    with fw as f:
        f.write(comment)
    # fw.write(comment)
    fw.close()

if __name__ == "__main__":
    print("----------starting procedure---------")
    file = "depend.txt"
    str = readfile(file)
    writefile(file, str)
    print("----------termination routine--------")

执行脚本

代码语言:javascript
复制
python replace.py

 3、bat脚本下载

download.bat

代码语言:javascript
复制
@echo off
REM for /f %%i in ('cat depend.txt') do echo "pip download %%i"
for /f %%i in ('cat depend.txt') do echo %%i
for /f %%i in ('cat depend.txt') do pip download %%i
pause

4、文件目录

脚本说明

depend.txt:将本机python依赖包写入depend.txt replace.py:将depend.txt替换成pip识别的版本名 download.bat:批量下载本机python离线安装包脚本

执行前

执行后

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-03-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 问题描述
  • 问题解决
  • 详细步骤
    • 1、将本机依赖导入txt
      • 2、去除依赖版本
        •  3、bat脚本下载
          • 4、文件目录
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档