确保要发布的包(demo)的根目录中有__main__.py文件,这个是程序执行入口。
编译
python3 -O -m compileall demo
批量改名.pyc文件
find . -name '*.pyc' -exec rename 's/.cpython-35.opt-1//' {} \;
移动.pyc文件
find . -name '*.pyc' -execdir mv {} .. \;
清理.py文件
find . -name '*.py' -type f -print -exec rm {} \;
清理__pycache__文件夹
find . -name '__pycache__' -exec rmdir {} \;
打包成zip
zip -r pub.zip ./demo/*
运行时只要将zip文件作为参数即可
python3 pub.zip
最终整合脚本
cd $1
python3 -O -m compileall .
find . -name '*.pyc' -exec rename 's/.cpython-35.opt-1//' {} \;
find . -name '*.pyc' -execdir mv {} .. \;
find . -name '*.py' -type f -print -exec rm {} \;
find . -name '__pycache__' -exec rmdir {} \;
zip -r ../$1.zip ./*
调用方式
chmod +x pycompile.sh
./pycompile.sh demo