python编译py成pyc和pyo
pyc是一种二进制文件,是由py文件经过编译后,生成的文件,是一种byte code,py文件变成pyc文件后,加载的速度有所提高,而且pyc是一种跨平台的字节码,是由python的虚拟机来执行的,这个是类似于JAVA或 者.NET的虚拟机的概念。pyc的内容,是跟python的版本相关的,不同版本编译后的pyc文件是不同的,2.5编译的pyc文件,2.4版本的 python是无法执行的。
1.编译单个py文件
(1)
直接在命令行下执行 python -m py_compile file.py
(2)
在python代码中使用py_compile.compile函数来编译。该函数的帮助信息如下:
Help on function compile in module py_compile: compile(file, cfile=None, dfile=None, doraise=False) Byte-compile one Python source file to Python bytecode. Arguments: file: source filename cfile: target filename; defaults to source with 'c' or 'o' appended ('c' normally, 'o' in optimizing mode, giving .pyc or .pyo) dfile: purported filename; defaults to source (this is the filename that will show up in error messages) doraise: flag indicating whether or not an exception should be raised when a compile error is found. If an exception occurs and this flag is set to False, a string indicating the nature of the exception will be printed, and the function will return to the caller. If an exception occurs and this flag is set to True, a PyCompileError exception will be raised. Note that it isn't necessary to byte-compile Python modules for execution efficiency -- Python itself byte-compiles a module when it is loaded, and if it can, writes out the bytecode to the corresponding .pyc (or .pyo) file. However, if a Python installation is shared between users, it is a good idea to byte-compile all modules upon installation, since other users may not be able to write in the source directories, and thus they won't be able to write the .pyc/.pyo file, and then they would be byte-compiling every module each time it is loaded. This can slow down program start-up considerably. See compileall.py for a script/module that uses this module to byte-compile all installed files (or all files in selected directories).
[baichuan@zjdw-odmz-0009 python]$ ls adp_group_info_data adp_group_info.py db_api.py db_api.pyc dump_rmc_area_ip.py dump_rmc_tag.py [baichuan@zjdw-odmz-0009 python]$ python Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import py_compile >>> py_compile.compile("adp_group_info.py") >>> py_compile.compile("/home/baichuan/yxc/python/dump_rmc_tag.py") >>> quit() [baichuan@zjdw-odmz-0009 python]$ ls adp_group_info_data adp_group_info.py adp_group_info.pyc db_api.py db_api.pyc dump_rmc_area_ip.py dump_rmc_tag.py dump_rmc_tag.pyc [baichuan@zjdw-odmz-0009 python]$
2.批量生成pyc文件
在python代码中使用compileall.compile_dir函数来编译。该函数的帮助信息如下:
Help on function compile_dir in module compileall: compile_dir(dir, maxlevels=10, ddir=None, force=0, rx=None, quiet=0) Byte-compile all modules in the given directory tree. Arguments (only dir is required): dir: the directory to byte-compile maxlevels: maximum recursion level (default 10) ddir: if given, purported directory name (this is the directory name that will show up in error messages) force: if 1, force compilation, even if timestamps are up-to-date quiet: if 1, be quiet during compilation
[baichuan@zjdw-odmz-0009 python]$ ls adp_group_info_data adp_group_info.py db_api.py dump_rmc_area_ip.py dump_rmc_tag.py [baichuan@zjdw-odmz-0009 python]$ python Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import compileall >>> compileall.compile_dir("/home/baichuan/yxc/python/") Listing /home/baichuan/yxc/python/ ... Compiling /home/baichuan/yxc/python/adp_group_info.py ... Compiling /home/baichuan/yxc/python/db_api.py ... Compiling /home/baichuan/yxc/python/dump_rmc_area_ip.py ... Compiling /home/baichuan/yxc/python/dump_rmc_tag.py ... 1 >>> quit() [baichuan@zjdw-odmz-0009 python]$ ls adp_group_info_data adp_group_info.py adp_group_info.pyc db_api.py db_api.pyc dump_rmc_area_ip.py dump_rmc_area_ip.pyc dump_rmc_tag.py dump_rmc_tag.pyc [baichuan@zjdw-odmz-0009 python]$