Heim  >  Artikel  >  Backend-Entwicklung  >  Pyhthon中使用compileall模块编译源文件为pyc文件

Pyhthon中使用compileall模块编译源文件为pyc文件

WBOY
WBOYOriginal
2016-06-10 15:14:061788Durchsuche

有的时候我们需要把项目中.py的python所有源文件编译成.pyc文件,只保留.pyc文件然后发布给别人(虽然说可以反编译,但也算是一种保护把).
这个时候就可以使用compileall 库来完成这个工作,它可以递归的把一个文件夹下的所有.py文件编译成.pyc文件。

例如我有一个django项目在test文件夹下就可以

复制代码 代码如下:

E:\>python -c "import compileall; import re; compileall.compile_dir('test', rx=re.compile(r'[/\\][.]svn'), force=True)" 

更多的使用和参数请参考
http://pymotw.com/2/compileall/
https://docs.python.org/2/library/compileall.html

然后就是要删除.py文件保留.pyc 文件了。
在linux下可以直接使用 find命令,在windows下可以使用dos命令,还可以写一个python脚本删除这些文件

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:Django中几种重定向方法Nächster Artikel:详解Python的单元测试