断网环境下,安装python环境依赖包(python环境迁移)

A电脑写的python脚本,现在要放到B电脑上去执行,遇到的第一个问题是如何搭建一个和a一模一样的环境:解决相关依赖

原博客:https://www.aliyun.com/jiaocheng/517065.html

如下有写好的成品脚本,执行即可:如果有用点个赞!!!

 

(1)使用方法 A电脑打开cmd 执行python SolvePythonDependency.py down

 生成  Dependency.zip 文件

(2)将SolvePythonDependency.py脚本 和Dependency.zip拷到 B电脑的目录下双击执行

 

如果有用点个赞!!!

成品脚本,复制保存为SolvePythonDependency.py

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

#!/usr/bin/env python
#coding=utf-8
import os
import zipfile
import shutil
import sys
import time
import subprocess
'''
   1.mode = (yes) :联网情况下的依赖解决 (no) :未联网情况下的依赖解决 (down):已有环境包的复制
'''
class SolvePythonDependency:
    #解决window下python包的依赖问题
    isPerpare = True


    def PrepareCheck(self):
        "当前文件下是否存在依赖包 Dependency.zip和依赖文件Requirement.txt"
        info = "|".join(os.listdir("./"))
        if "Dependency.zip" not in info :
            print "can`t find file:","Dependency.zip"
            self.isPerpare = False
            return
        z = zipfile.ZipFile("Dependency.zip")
        z.extractall()
        z.close()


    def AfterDetail(self):
        if os.path.exists("Dependency"):
            shutil.rmtree("Dependency")
        if os.path.exists("Requirements.txt"):
            os.remove("Requirements.txt")
        print "anykey to exit"


    def PrepareDownload(self):
        "准备依赖文件和依赖的包"
        os.system("pip freeze >Requirements.txt")
        targetFile = "Dependency"


        if os.path.exists("./%s"% targetFile):
            shutil.rmtree(targetFile)


        os.mkdir(targetFile)
        child = subprocess.Popen("pip install --download ./%s -r Requirements.txt " % targetFile)
        child.wait()


        # makezip
    
        z = zipfile.ZipFile("%s.zip" % targetFile,'w')
        z.write("./Requirements.txt")
        for root, subfolders, subfileLists in os.walk(r'./Dependency'):
            #  filesname是一个列表,我们需要里面的每个文件名和当前路径组合
            for file in subfileLists:
                # 将当前路径与当前路径下的文件名组合,就是当前文件的绝对路径
                z.write(os.path.join(root, file))
        z.close()




    def ConnectI(self):
        "联网情况下"
        os.system("pip install -r Requirements.txt")


    def NoConnectI(self):
        "不联网情况下"
        os.system("pip install --no-index --find-links=Dependency -r Requirements.txt ")


    def install_with_Net(self):
        "联网情况下"
        self.PrepareCheck()
        if self.isPerpare :
            self.ConnectI()
        self.AfterDetail()
    def install_without_Net(self):
        "联网情况下"
        self.PrepareCheck()
        if self.isPerpare :
            self.NoConnectI()
        self.AfterDetail()


def main():
    if len(sys.argv) == 1 :
        mode = "no"
    else:
        mode = sys.argv[1]


    solvePythonDependency = SolvePythonDependency()


    if mode == "yes":
        print 'install with internet'
        solvePythonDependency.install_with_Net()
    elif mode == "no":
        print 'install without internet'
        solvePythonDependency.install_without_Net()
    elif mode == "down":
        print 'produce \'Denpendency.zip\''
        solvePythonDependency.PrepareDownload()
        solvePythonDependency.AfterDetail()
    else:
        print 'parament error not in [yes,no,down]'


if __name__ == '__main__':
    try:
        main()
    except BaseException ,e:
        print e.message
        raw_input()


    raw_input()

 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值