Chinaunix首页 | 论坛 | 博客
  • 博客访问: 527419
  • 博文数量: 96
  • 博客积分: 2960
  • 博客等级: 少校
  • 技术积分: 1850
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-11 15:25
文章分类

全部博文(96)

文章存档

2009年(37)

2008年(59)

我的朋友

分类: Python/Ruby

2008-09-10 16:25:41

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 () (cross-platform), (Windows), (cross-platform), or Bob Ippolito’s (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, might be what you need. For Windows, my 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 :

import py_compile

py_compile.compile("mymodule.py")

There’s also 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 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.

阅读(1423) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~