我要实现将python编译成pyc,并且打包到zip,这样就可以直接运行
实现的python脚本如下build.py
dir="baiduindex"
cversion=".cpython-37"
pycDir=dir+'/__pycache__'
buildDir='build'
destZip='build.zip'
import os
import shutil
import compileall
import zipfile
if os.path.exists(pycDir):
shutil.rmtree(pycDir)
if os.path.exists(buildDir):
shutil.rmtree(buildDir)
if os.path.exists(destZip):
os.remove(destZip)
if not os.path.exists(buildDir):
os.makedirs(buildDir)
compileall.compile_dir(dir)
names = os.listdir(pycDir)
zp=zipfile.ZipFile(destZip,'w', zipfile.ZIP_DEFLATED)
for name in names:
destName=name.replace(cversion,"")
shutil.copyfile(pycDir+"/"+name,buildDir+"/"+destName)
zp.write(buildDir+"/"+destName,destName)
zp.close()
shutil.rmtree(buildDir)
运行build.py就能产生一个build.zip
只要目录下面有__main__.py,build.zip就能直接运行
python build.zip
在服务器上运行还需要记录日志和后台运行
nohup python -u build.zip > log.log 2>&1 &