适合单py文件的pyinstaller一键自动懒人打包
【说明】:自动搜寻同路径下的py文件,调用终端使用pyinstaller进行打包,将打包后的可执行文件从dist文件夹移动到当前目录,并移除dist、build文件夹和spec文件
【使用注意】:1. 需要pyinstaller;2. 当前目录只有一个py文件
【使用方法】:放在需要打包的py文件相同目录下运行
代码:
import os
import shutil
def upgrade():
pyfilename = [f for f in os.listdir()
if '.py' in f and f != os.path.basename(__file__)][0]
filename = pyfilename[:-3]
# terminal
# 视情况修改
c = f'python D:/python/Scripts/pyinstaller.exe -F {pyfilename}'
os.system(c)
if os.path.exists(f'{filename}.exe'):
os.remove(f'{filename}.exe')
move_file_path = f'dist/{filename}.exe'
move_path = os.getcwd()
shutil.move(move_file_path, move_path)
shutil.rmtree('dist')
shutil.rmtree('build')
os.remove(f'{filename}.spec')
upgrade()