免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1890 | 回复: 0
打印 上一主题 下一主题

Compiling Python Code [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-09-10 16:25 |只看该作者 |倒序浏览
Compiling Python Code  (maybe tooooooooooold)
Python source code is automatically compiled into Python byte code
by the CPython interpreter.  Compiled code is usually stored in PYC (or
PYO) files, and is regenerated when the source is updated, or when
otherwise necessary.
To distribute a program to people who already have Python installed,
you can ship either the PY files or the PYC files.  In recent versions,
you can also create a ZIP archive containing PY or PYC files, and use
a small “bootstrap script” to add that ZIP archive to the path.
To “compile” a Python program into an executable, use a bundling
tool, such as
Gordon McMillan’s installer
(
alternative download
) (cross-platform),
Thomas Heller’s py2exe
(Windows),
Anthony Tuininga’s cx_Freeze
(cross-platform),
or Bob Ippolito’s
py2app
(Mac).  These tools
puts your modules and data files in some kind of archive file, and creates
an executable that automatically sets things up so that modules are
imported from that archive.  Some tools can embed the archive in the
executable itself.
If all you need is to wrap up a couple of Python scripts and
modules into a single file,
Squeeze
might be what
you need.  For Windows, my
ExeMaker
tool can also
be quite useful (on its own, or in combination with squeeze).
Compiling Python modules to byte code 
#
Python automatically compiles Python source code when you import
a module, so the easiest way to create a PYC file is to import it.
If you have a module mymodule.py, just do:
>>> import mymodule
to create a mymodule.pyc file in the same directory.
A drawback is that it doesn’t only compile the module, it also
executes it, which may not be what you want.  (however, it does
compile the entire script even if it fails to execute the script).
To do this programmatically, and without executing the code,
you can use the py_compile
module:
import py_compile
py_compile.compile("mymodule.py")
There’s also a compileall
module which can be used to compile all modules in an entire directory
tree.
import compileall
compileall.compile_dir("mylib", force=1)More on byte code 
#
Python’s byte code is portable between platforms, but not necessarily
between Python releases.  The imp.get_magic() function returns a
4-byte string identifying the byte code format used by the current interpreter.
You can use the compile function and
the marshal module
to compile Python code into code objects, and convert such code objects
to binary strings.  To reverse this process, use marshal to convert
from strings to code, and use exec to execute code.
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/28158/showart_1183149.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP