全文基于个人本地路径及喜好,请使用的时候按照自己喜爱的路径设置。
一、分段代码:
1.轮子:
import os,sys
import requests
import zipfile
import subprocess
2.打包代码:
#打包部分0↓
def nuitkaing():
windows_icon = ''
if windows_icon:
icon = ' --windows-icon-from-ico=' + windows_icon
else:
icon = ''
file_version = '1.0.0.0'
if file_version:
f_version = ' --windows-file-version=' + file_version
else:
f_version = ''
company_name = 'wUw(wuwofcproject.com)'
if company_name:
c_name = ' --windows-company-name=' + str(company_name)
else:
c_name = ''
file_description = 'Copyright2022wUw(wuwofcproject.com)'
if file_description:
f_description = ' --windows-file-description=' + file_description
else:
f_description = ''
output_dir = 'CopyII'
if output_dir:
o_dir = ' --output-dir=' + output_dir
else:
o_dir = ''
pythonfile = ' CopyII.py'
subprocess.run('nuitka --mingw64 --standalone '
'--windows-disable-console '
'--show-progress '
'--show-memory '
'--plugin-enable=tk-inter '
'--windows-file-version='
+ icon
+ f_version
+ c_name
+ f_description
+ ' --follow-imports'
+ o_dir
+ pythonfile)
print('over')
return
#打包部分↑
3.判断是否已有环境:
rig = subprocess.getstatusoutput("gcc -v")#cmdgcc-v,错误则返回非0值,判断是否已经设置好环境
if (rig[0]) == 0:
nuitkaing()
4.设置环境:
4.1gcc部分:
else:
if 'mingw64.zip' in os.listdir(r'D:/ProgramData/'):#看看文件夹下是否包含已经下载好的minge64
pass
else:
#1按照url下载zip压缩包↓
print('downloading...')
try:
#设置URL,现在这里是10.0.0-r3
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
if not os.path.exists(r'D:\ProgramData'):
os.makedirs(r'D:\ProgramData')
with open(r'D:/ProgramData/' + 'mingw64.zip', "wb") as f:
f.write(url)
print('ok')
except:
print('移不动,联不通,信不过')
sys.exit()
#1下载结束↑
#2以下开始解压缩↓
frzip = zipfile.ZipFile(r'D:/ProgramData/' + 'mingw64.zip', 'r')
print('loading..')
for file in frzip.namelist():
frzip.extract(file, r'D:/ProgramData/mingw__64')
frzip.close()
print('ok')
#2解压缩完毕↑
#3查找bin子文件夹有多深(此处只设计二级)↓
folder = os.listdir(r'D:/ProgramData/mingw__64')
if 'bin' in folder:
addressp = r'D:/ProgramData/mingw__64/bin'
addressm = r'D:/ProgramData/mingw__64'
else:
for t in folder:
folder2 = os.listdir(r'D:/ProgramData/mingw__64/' + t)
if 'bin' in folder2:
addressp = r'D:/ProgramData/mingw__64/' + t + '/bin'
addressm = r'D:/ProgramData/mingw__64/' + t
print('add:', addressp, addressm)
#3查找完毕,将路径固定↑
env1 = os.environ.get('path') # 读取path里的值
env1 = tuple(env1.split(';')) # 按照;分隔
env2 = os.environ.get('MinGW_64x') # 读取MinGW_64里的值
print('\n'.join(env1))
print(env2)
if addressp in env1:#如果path里包含addressp路径
pass
else:
os.putenv('path', addressp)
if addressm in env2:#如果MinGW_64x里包含addressm路径
pass
else:
os.putenv('MinGW_64x', addressm)
# os.putenv('path',r'D:\ProgramData\mingw64')
wor = subprocess.getstatusoutput("gcc -v") # cmdgcc-v,错误则返回非0值
if (wor[0]) == 0:
print('完成环境设置')
else:
print('警报!警报!警报!\na:妖兽啦~\nb:好机车诶。')
4.2安装nuitka:
worr = subprocess.getstatusoutput("conda install nuitka=0.6.16")#conda安装包
#worr = subprocess.getstatusoutput("pip install nuitka==0.6.16")#pip安装包
if (worr[0]) == 0:
print('完成,即将打包')
else:
print('安装出错')
nuitkaing()
二、完整代码:
import os,sys
import requests
import zipfile
import subprocess
#打包部分0↓
def nuitkaing():
windows_icon = ''
if windows_icon:
icon = ' --windows-icon-from-ico=' + windows_icon
else:
icon = ''
file_version = '1.0.0.0'
if file_version:
f_version = ' --windows-file-version=' + file_version
else:
f_version = ''
company_name = 'wUw(wuwofcproject.com)'
if company_name:
c_name = ' --windows-company-name=' + str(company_name)
else:
c_name = ''
file_description = 'Copyright2022wUw(wuwofcproject.com)'
if file_description:
f_description = ' --windows-file-description=' + file_description
else:
f_description = ''
output_dir = 'CopyII'
if output_dir:
o_dir = ' --output-dir=' + output_dir
else:
o_dir = ''
pythonfile = ' CopyII.py'
subprocess.run('nuitka --mingw64 --standalone '
'--windows-disable-console '
'--show-progress '
'--show-memory '
'--plugin-enable=tk-inter '
'--windows-file-version='
+ icon
+ f_version
+ c_name
+ f_description
+ ' --follow-imports'
+ o_dir
+ pythonfile)
print('over')
return
#打包部分↑
rig = subprocess.getstatusoutput("gcc -v")#cmdgcc-v,错误则返回非0值,判断是否已经设置好环境
if (rig[0]) == 0:
nuitkaing()
else:
if 'mingw64.zip' in os.listdir(r'D:/ProgramData/'):#看看文件夹下是否包含已经下载好的minge64
pass
else:
#1按照url下载zip压缩包↓
print('downloading...')
try:
#设置URL,现在这里是10.0.0-r3
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
if not os.path.exists(r'D:\ProgramData'):
os.makedirs(r'D:\ProgramData')
with open(r'D:/ProgramData/' + 'mingw64.zip', "wb") as f:
f.write(url)
print('ok')
except:
print('移不动,联不通,信不过')
sys.exit()
#1下载结束↑
#2以下开始解压缩↓
frzip = zipfile.ZipFile(r'D:/ProgramData/' + 'mingw64.zip', 'r')
print('loading..')
for file in frzip.namelist():
frzip.extract(file, r'D:/ProgramData/mingw__64')
frzip.close()
print('ok')
#2解压缩完毕↑
#3查找bin子文件夹有多深(此处只设计二级)↓
folder = os.listdir(r'D:/ProgramData/mingw__64')
if 'bin' in folder:
addressp = r'D:/ProgramData/mingw__64/bin'
addressm = r'D:/ProgramData/mingw__64'
else:
for t in folder:
folder2 = os.listdir(r'D:/ProgramData/mingw__64/' + t)
if 'bin' in folder2:
addressp = r'D:/ProgramData/mingw__64/' + t + '/bin'
addressm = r'D:/ProgramData/mingw__64/' + t
print('add:', addressp, addressm)
#3查找完毕,将路径固定↑
env1 = os.environ.get('path') # 读取path里的值
env1 = tuple(env1.split(';')) # 按照;分隔
env2 = os.environ.get('MinGW_64x') # 读取MinGW_64里的值
print('\n'.join(env1))
print(env2)
if addressp in env1:#如果path里包含addressp路径
pass
else:
os.putenv('path', addressp)
if addressm in env2:#如果MinGW_64x里包含addressm路径
pass
else:
os.putenv('MinGW_64x', addressm)
# os.putenv('path',r'D:\ProgramData\mingw64')
wor = subprocess.getstatusoutput("gcc -v") # cmdgcc-v,错误则返回非0值
if (wor[0]) == 0:
print('完成环境设置')
else:
print('警报!警报!警报!\na:妖兽啦~\nb:好机车诶。')
worr = subprocess.getstatusoutput("conda install nuitka=0.6.16")#conda安装包
#worr = subprocess.getstatusoutput("pip install nuitka==0.6.16")#pip安装包
if (worr[0]) == 0:
print('完成,即将打包')
else:
print('安装出错')
nuitkaing()