python Django项目通过编译为.pyc 形式发布

您所在的位置:网站首页 py文件编译为pyc python Django项目通过编译为.pyc 形式发布

python Django项目通过编译为.pyc 形式发布

2024-07-12 19:10:08| 来源: 网络整理| 查看: 265

文章目录 一、批量编译代码二、运行命令三、注意

一、批量编译代码

创建compile_pyc.py,代码如下:

import os import sys import shutil from py_compile import compile def clean(path): for parent, dirname, filename in os.walk(path): for dir in dirname: if dir == '__pycache__': try: fullname = os.path.join(parent, dir) shutil.rmtree(fullname) print("Success clean Folder:%s" % fullname) except Exception as e: print("Can't clean Folder:%s, reason:%s" % (fullname, e)) def compile_pyc(path): for parent, dirname, filename in os.walk(path): for cfile in filename: fullname = os.path.join(parent, cfile) if cfile[-3:] == '.py': try: if compile(fullname): if cfile != 'settings.py' and cfile != 'wsgi.py': os.remove(fullname) #删除原文件,保留settings.py和wsgi.py print("Success compile and remove file:%s" % fullname) else: print("Can't compile file:%s,The original file has been retained" % fullname) except Exception as e: print("Can't compile file:%s, reason:%s" % (fullname, e)) def move(path): for parent, dirname, filename in os.walk(path): for c_file in filename: fullname = os.path.join(parent, c_file) if c_file[-4:] == '.pyc': try: parent_path = os.path.dirname(parent) shutil.move(fullname, parent_path) print('update the dir of file successfully') except Exception as e: print("Can't move file:%s, reason:%s" % (fullname, e)) def replace_name(path): for parent, dirname, filename in os.walk(path): for c_file in filename: fullname = os.path.join(parent, c_file) if c_file[-4:] == '.pyc': try: cfile_name = '' cfile_list = c_file.split('.') version = sys.version_info replace_name = 'cpython-' + str(version[0])+str(version[1]) for i in range(len(cfile_list)): if cfile_list[i] == replace_name: continue cfile_name += cfile_list[i] if i == len(cfile_list) - 1: continue cfile_name += '.' shutil.move(fullname, os.path.join(parent, cfile_name)) print('update the name of the file successfully') except Exception as e: print("Can't remove file:%s, reason:%s" % (fullname, e)) if __name__ == '__main__': if len(sys.argv) == 3: cmd = sys.argv[1] path = sys.argv[2] if os.path.exists(path) and os.path.isdir(path): if cmd == 'clean': clean(path) elif cmd == 'move': move(path) elif cmd == 'replace_name': replace_name(path) elif cmd == 'compile_pyc': compile_pyc(path) elif cmd == 'compile': clean(path) compile_pyc(path) move(path) replace_name(path) clean(path) else: print('没有该命令') else: print("Not an directory or Direcotry doesn't exist!") else: print("在命令行中使用以下命令:") print("\tpython3 compile_pyc.py compile_pyc PATH\t\t#生成pyc文件") print("\tpython3 compile_pyc.py move PATH\t\t#移动所有pyc文件至原位置") print("\tpython3 compile_pyc.py replace_name PATH\t\t#修改文件名文件") print("\tpython3 compile_pyc.py clean PATH\t\t#清除当前项目中所有的__pycache__文件夹及以内文件") print("\tpython3 compile_pyc.py compile PATH\t\t#一键编译pyc") 二、运行命令

逐步运行以下命令

python3 compile_pyc.py clean PATH #清除当前项目中所有的__pycache__文件夹及以内文件 python3 compile_pyc.py compile_pyc PATH #生成.pyc文件,并删除源文件 python3 compile_pyc.py move PATH #将.pyc文件移动到原py文件目录下 python3 compile_pyc.py replace_name PATH #去掉.pyc文件名中的版本号 python3 compile_pyc.py clean PATH

或者直接运行以下一条命令就可以

python3 compile_pyc.py compile PATH 三、注意

保留了settings.py和wsgi.py这两个文件,方便改变配置,以及之后的uwsgi部署

多次运行,.pyc会移动错乱,保证只移动在__pycache__下的.pyc文件,修改move代码

def move(path): for parent, dirname, filename in os.walk(path): for c_file in filename: fullname = os.path.join(parent, c_file) if os.path.basename(parent) == '__pycache__': if c_file[-4:] == '.pyc': try: parent_path = os.path.dirname(parent) shutil.move(fullname, parent_path) print('update the dir of file successfully') except Exception as e: print("Can't move file:%s, reason:%s" % (fullname, e))


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭