全文基于个人本地路径及喜好,请使用的时候按照自己喜爱的路径设置。

一、分段代码:

1.轮子:

  1. import os,sys
  2. import requests
  3. import zipfile
  4. import subprocess

2.打包代码:

  1. #打包部分0↓
  2. def nuitkaing():
  3. windows_icon = ''
  4. if windows_icon:
  5. icon = ' --windows-icon-from-ico=' + windows_icon
  6. else:
  7. icon = ''
  8. file_version = '1.0.0.0'
  9. if file_version:
  10. f_version = ' --windows-file-version=' + file_version
  11. else:
  12. f_version = ''
  13. company_name = 'wUw(wuwofcproject.com)'
  14. if company_name:
  15. c_name = ' --windows-company-name=' + str(company_name)
  16. else:
  17. c_name = ''
  18. file_description = 'Copyright2022wUw(wuwofcproject.com)'
  19. if file_description:
  20. f_description = ' --windows-file-description=' + file_description
  21. else:
  22. f_description = ''
  23. output_dir = 'CopyII'
  24. if output_dir:
  25. o_dir = ' --output-dir=' + output_dir
  26. else:
  27. o_dir = ''
  28. pythonfile = ' CopyII.py'
  29. subprocess.run('nuitka --mingw64 --standalone '
  30. '--windows-disable-console '
  31. '--show-progress '
  32. '--show-memory '
  33. '--plugin-enable=tk-inter '
  34. '--windows-file-version='
  35. + icon
  36. + f_version
  37. + c_name
  38. + f_description
  39. + ' --follow-imports'
  40. + o_dir
  41. + pythonfile)
  42. print('over')
  43. return
  44. #打包部分↑

3.判断是否已有环境:

  1. rig = subprocess.getstatusoutput("gcc -v")#cmdgcc-v,错误则返回非0值,判断是否已经设置好环境
  2. if (rig[0]) == 0:
  3. nuitkaing()

4.设置环境:

4.1gcc部分:

  1. else:
  2. if 'mingw64.zip' in os.listdir(r'D:/ProgramData/'):#看看文件夹下是否包含已经下载好的minge64
  3. pass
  4. else:
  5. #1按照url下载zip压缩包↓
  6. print('downloading...')
  7. try:
  8. #设置URL,现在这里是10.0.0-r3
  9. url = requests.get(r'https://github.com/brechtsanders/winlibs_mingw/releases/download/11.3.0-14.0.3-10.0.0-ucrt-r3/winlibs-x86_64-posix-seh-gcc-11.3.0-llvm-14.0.3-mingw-w64ucrt-10.0.0-r3.zip').content
  10. if not os.path.exists(r'D:\ProgramData'):
  11. os.makedirs(r'D:\ProgramData')
  12. with open(r'D:/ProgramData/' + 'mingw64.zip', "wb") as f:
  13. f.write(url)
  14. print('ok')
  15. except:
  16. print('移不动,联不通,信不过')
  17. sys.exit()
  18. #1下载结束↑
  19. #2以下开始解压缩↓
  20. frzip = zipfile.ZipFile(r'D:/ProgramData/' + 'mingw64.zip', 'r')
  21. print('loading..')
  22. for file in frzip.namelist():
  23. frzip.extract(file, r'D:/ProgramData/mingw__64')
  24. frzip.close()
  25. print('ok')
  26. #2解压缩完毕↑
  27. #3查找bin子文件夹有多深(此处只设计二级)↓
  28. folder = os.listdir(r'D:/ProgramData/mingw__64')
  29. if 'bin' in folder:
  30. addressp = r'D:/ProgramData/mingw__64/bin'
  31. addressm = r'D:/ProgramData/mingw__64'
  32. else:
  33. for t in folder:
  34. folder2 = os.listdir(r'D:/ProgramData/mingw__64/' + t)
  35. if 'bin' in folder2:
  36. addressp = r'D:/ProgramData/mingw__64/' + t + '/bin'
  37. addressm = r'D:/ProgramData/mingw__64/' + t
  38. print('add:', addressp, addressm)
  39. #3查找完毕,将路径固定↑
  40. env1 = os.environ.get('path') # 读取path里的值
  41. env1 = tuple(env1.split(';')) # 按照;分隔
  42. env2 = os.environ.get('MinGW_64x') # 读取MinGW_64里的值
  43. print('\n'.join(env1))
  44. print(env2)
  45. if addressp in env1:#如果path里包含addressp路径
  46. pass
  47. else:
  48. os.putenv('path', addressp)
  49. if addressm in env2:#如果MinGW_64x里包含addressm路径
  50. pass
  51. else:
  52. os.putenv('MinGW_64x', addressm)
  53. # os.putenv('path',r'D:\ProgramData\mingw64')
  54. wor = subprocess.getstatusoutput("gcc -v") # cmdgcc-v,错误则返回非0值
  55. if (wor[0]) == 0:
  56. print('完成环境设置')
  57. else:
  58. print('警报!警报!警报!\na:妖兽啦~\nb:好机车诶。')

4.2安装nuitka:

  1. worr = subprocess.getstatusoutput("conda install nuitka=0.6.16")#conda安装包
  2. #worr = subprocess.getstatusoutput("pip install nuitka==0.6.16")#pip安装包
  3. if (worr[0]) == 0:
  4. print('完成,即将打包')
  5. else:
  6. print('安装出错')
  7. nuitkaing()

二、完整代码:

  1. import os,sys
  2. import requests
  3. import zipfile
  4. import subprocess
  5. #打包部分0↓
  6. def nuitkaing():
  7. windows_icon = ''
  8. if windows_icon:
  9. icon = ' --windows-icon-from-ico=' + windows_icon
  10. else:
  11. icon = ''
  12. file_version = '1.0.0.0'
  13. if file_version:
  14. f_version = ' --windows-file-version=' + file_version
  15. else:
  16. f_version = ''
  17. company_name = 'wUw(wuwofcproject.com)'
  18. if company_name:
  19. c_name = ' --windows-company-name=' + str(company_name)
  20. else:
  21. c_name = ''
  22. file_description = 'Copyright2022wUw(wuwofcproject.com)'
  23. if file_description:
  24. f_description = ' --windows-file-description=' + file_description
  25. else:
  26. f_description = ''
  27. output_dir = 'CopyII'
  28. if output_dir:
  29. o_dir = ' --output-dir=' + output_dir
  30. else:
  31. o_dir = ''
  32. pythonfile = ' CopyII.py'
  33. subprocess.run('nuitka --mingw64 --standalone '
  34. '--windows-disable-console '
  35. '--show-progress '
  36. '--show-memory '
  37. '--plugin-enable=tk-inter '
  38. '--windows-file-version='
  39. + icon
  40. + f_version
  41. + c_name
  42. + f_description
  43. + ' --follow-imports'
  44. + o_dir
  45. + pythonfile)
  46. print('over')
  47. return
  48. #打包部分↑
  49. rig = subprocess.getstatusoutput("gcc -v")#cmdgcc-v,错误则返回非0值,判断是否已经设置好环境
  50. if (rig[0]) == 0:
  51. nuitkaing()
  52. else:
  53. if 'mingw64.zip' in os.listdir(r'D:/ProgramData/'):#看看文件夹下是否包含已经下载好的minge64
  54. pass
  55. else:
  56. #1按照url下载zip压缩包↓
  57. print('downloading...')
  58. try:
  59. #设置URL,现在这里是10.0.0-r3
  60. url = requests.get(r'https://github.com/brechtsanders/winlibs_mingw/releases/download/11.3.0-14.0.3-10.0.0-ucrt-r3/winlibs-x86_64-posix-seh-gcc-11.3.0-llvm-14.0.3-mingw-w64ucrt-10.0.0-r3.zip').content
  61. if not os.path.exists(r'D:\ProgramData'):
  62. os.makedirs(r'D:\ProgramData')
  63. with open(r'D:/ProgramData/' + 'mingw64.zip', "wb") as f:
  64. f.write(url)
  65. print('ok')
  66. except:
  67. print('移不动,联不通,信不过')
  68. sys.exit()
  69. #1下载结束↑
  70. #2以下开始解压缩↓
  71. frzip = zipfile.ZipFile(r'D:/ProgramData/' + 'mingw64.zip', 'r')
  72. print('loading..')
  73. for file in frzip.namelist():
  74. frzip.extract(file, r'D:/ProgramData/mingw__64')
  75. frzip.close()
  76. print('ok')
  77. #2解压缩完毕↑
  78. #3查找bin子文件夹有多深(此处只设计二级)↓
  79. folder = os.listdir(r'D:/ProgramData/mingw__64')
  80. if 'bin' in folder:
  81. addressp = r'D:/ProgramData/mingw__64/bin'
  82. addressm = r'D:/ProgramData/mingw__64'
  83. else:
  84. for t in folder:
  85. folder2 = os.listdir(r'D:/ProgramData/mingw__64/' + t)
  86. if 'bin' in folder2:
  87. addressp = r'D:/ProgramData/mingw__64/' + t + '/bin'
  88. addressm = r'D:/ProgramData/mingw__64/' + t
  89. print('add:', addressp, addressm)
  90. #3查找完毕,将路径固定↑
  91. env1 = os.environ.get('path') # 读取path里的值
  92. env1 = tuple(env1.split(';')) # 按照;分隔
  93. env2 = os.environ.get('MinGW_64x') # 读取MinGW_64里的值
  94. print('\n'.join(env1))
  95. print(env2)
  96. if addressp in env1:#如果path里包含addressp路径
  97. pass
  98. else:
  99. os.putenv('path', addressp)
  100. if addressm in env2:#如果MinGW_64x里包含addressm路径
  101. pass
  102. else:
  103. os.putenv('MinGW_64x', addressm)
  104. # os.putenv('path',r'D:\ProgramData\mingw64')
  105. wor = subprocess.getstatusoutput("gcc -v") # cmdgcc-v,错误则返回非0值
  106. if (wor[0]) == 0:
  107. print('完成环境设置')
  108. else:
  109. print('警报!警报!警报!\na:妖兽啦~\nb:好机车诶。')
  110. worr = subprocess.getstatusoutput("conda install nuitka=0.6.16")#conda安装包
  111. #worr = subprocess.getstatusoutput("pip install nuitka==0.6.16")#pip安装包
  112. if (worr[0]) == 0:
  113. print('完成,即将打包')
  114. else:
  115. print('安装出错')
  116. nuitkaing()