使用compileall来预编译python代码,compileallpython,python中有一个co
使用compileall来预编译python代码,compileallpython,python中有一个co
python中有一个compileall模块,此模块可以将指定目录下的.py文件编译成python的二进制文件.pyc或者.pyo文件。
compile_dir()
可以遍历目录然后做二进制的编译。
如下代码:
import compileallcompileall.compile_dir('examples')
上面的代码,默认情况下,深度小于10的子目录都会被编译。如果目录中包含svn目录的话会将svn目录下的.py文件也编译了。
$ python compileall_compile_dir.pyListing examples ...Listing examples/.svn ...Listing examples/.svn/prop-base ...Listing examples/.svn/props ...Listing examples/.svn/text-base ...Listing examples/.svn/tmp ...Listing examples/.svn/tmp/prop-base ...Listing examples/.svn/tmp/props ...Listing examples/.svn/tmp/text-base ...Compiling examples/a.py ...Listing examples/subdir ...Listing examples/subdir/.svn ...Listing examples/subdir/.svn/prop-base ...Listing examples/subdir/.svn/props ...Listing examples/subdir/.svn/text-base ...Listing examples/subdir/.svn/tmp ...Listing examples/subdir/.svn/tmp/prop-base ...Listing examples/subdir/.svn/tmp/props ...Listing examples/subdir/.svn/tmp/text-base ...Compiling examples/subdir/b.py ...
要过滤不必要的目录,可以使用rx参数提供一个正则表达式来排除不需要的目录。
import compileallimport recompileall.compile_dir('examples', rx=re.compile(r'/\.svn'))$ python compileall_exclude_dirs.pyListing examples ...Compiling examples/a.py ...Listing examples/subdir ...Compiling examples/subdir/b.py ...
maxlevels参数可以控制目录树递归的层级数,例如,不需要递归可以将maxlevels参数设置为0
import compileallimport recompileall.compile_dir('examples', maxlevels=0, rx=re.compile(r'/\.svn'))$ python compileall_recursion_depth.pyListing examples ...Compiling examples/a.py ...
编译sys.path中包含的所有目录:
通过调用compile_path()
可以编译sys.path中包含的所有目录
import compileallimport syssys.path[:] = ['examples', 'notthere']print 'sys.path =', sys.pathcompileall.compile_path()
上面的例子在编译之前重新设置了sys.path的值,这是为了防止compileall去尝试编译系统路径
$ python compileall_path.pysys.path = ['examples', 'notthere']Listing examples ...Compiling examples/a.py ...Listing notthere ...Can't list notthere
在命令行中使用compileall
如下使用示例:
$ python -m compileall -hoption -h not recognized
用法:: python compileall.py [-l] [-f] [-q] [-d destdir] [-x regexp] [directory ...]
-l: don't recurse down-f: force rebuild even if timestamps are up-to-date-q: quiet operation-d destdir: purported directory name for error messages,if no directory arguments, -l sys.path is assumed-x regexp: skip files matching the regular expression regexp the regexp is search for in the full path of the file如下例子,忽略.svn目录:
$ python -m compileall -x '/\.svn' examplesListing examples ...Compiling examples/a.py ...Listing examples/subdir ...Compiling examples/subdir/b.py ...
本文翻译自:http://doughellmann.com/2009/01/pymotw-compileall.html
相关内容
- python读取文件同时输出行号和内容,,file = open(
- Python实现二分查找(二分查询),python二分,本来想自己
- python字符串和整形的相互转换,python字符串整形,print
- AzDG可逆加密演算法 for Python,azdgpython,花了二天的時間
- 一个python编写的用于搜索文件并进行内容替换的类,
- python enumerate用法,pythonenumerate,enumerate是py
- Python 使用webbrowser模块来刷点击量,pythonwebbrowser,impor
- 用Python之for循环解决“韩信点兵问题”,python点兵,中国
- python清除字符串里的非数字字符,python字符串字符,im
- python 实现 数独算法,python实现算法,# -*- coding
评论关闭