设为首页 收藏本站
查看: 1111|回复: 0

[经验分享] python编译py成pyc和pyo

[复制链接]

尚未签到

发表于 2017-4-27 09:16:51 | 显示全部楼层 |阅读模式
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]$
 



 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-369763-1-1.html 上篇帖子: [转]理解python中的bytestring和unicode 下篇帖子: Python 与 C++ 的交互编程
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表