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]$
相关推荐
为了防止代码泄露就考虑不采用直接给源码方式,而python二进制脚本pyc和pyo,虽然提供的不是源码,但可以通过uncompyle2直接得到源码,所以现在用类似py2exe软件将py文件打包成exe,用此脚本可以将exe反编译为py脚本
这是个windows版的工具,可以反编译单个pyc,pyo 文件,或者选定反编译一个指定文件夹下面的pyc,pyo 文件, 确实很好用.
python 编译。py文件编译为pyc pyo等方法。值得学习。
编译py为so文件,更好的隐藏源码,可以直接编译整个python工程 删除.pyc .pyo文件 删除.py源文件 删除.c编译文件 也可以使用Makefile的形式: 1.将Makefile和工程目录放在同一目录下; 2.正确是安装Cpython解释器; ...
python并非完全是解释性语言,它是有编译的,先把源码py文件编译成pyc或者pyo,然后由python的虚拟机执行,相对于py文件来说,编译成pyc和pyo本质上和py没有太大区别,只是对于这个模块的加载速度提高了,并没有...
4、Python编译 python -O -m py_compile alp.py 执行结果:生成alp.pyo python -O -m py_compile alp.py 注:如果不带选项-O则生成的是pyc文件,-O选项则可以在生成代码时进行一定的优化。
由于生产部署的问题,需要把用python写的深度学习代码编译为可被调用的文件。....pyo Python源代码编译优化生成的字节码。pyo比pyc并没有优化多少,只是去掉了断言 .pyd Python的动态链接库(Window
4、一个library.zip文件,它包含了已编译的纯的python模块如.pyc或.pyo 上面的mysetup.py创建了一个控制台的helloword.exe程序,如果你要创建一个图形用户界的程序,那么你只需要将mysetup.py中的console=["hello...
python程序的扩展名有.py、.pyc、.pyo和.pyd。.py是源文件,.pyc是源文件编译后的文件,.pyo是源文件优化编译后的文件,.pyd是其他语言写的python库。 扩展名 在写Python程序时我们常见的扩展名是py, pyc,其实还有...
1. 手动制作python的exe可执行程序Python没有内建一个编译为exe的功能。给python程序的部署带来不少的麻烦。所以就会出现一些py2exe之类... 2) python从2.3开始,支持从zip文件中import库(支持.py,.pyc和.pyo,但不支
Python 2.7的反编译工具,它可以把python生成的pyo、pyc字节码文件反编译为十分完美的源码,并可以将反编译后的源码再次生成字节码文件!