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

WBOY
Release: 2016-06-10 15:14:06
Original
1786 people have browsed it

有的时候我们需要把项目中.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脚本删除这些文件

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!