Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 1 | THE FREEZE SCRIPT |
| 2 | ================= |
| 3 | |
| 4 | |
| 5 | What is Freeze? |
| 6 | --------------- |
| 7 | |
| 8 | Freeze make it possible to ship arbitrary Python programs to people |
| 9 | who don't have Python. The shipped file (called a "frozen" version of |
| 10 | your Python program) is an executable, so this only works if your |
| 11 | platform is compatible with that on the receiving end (this is usually |
| 12 | a matter of having the same major operating system revision and CPU |
| 13 | type). |
| 14 | |
| 15 | The shipped file contains a Python interpreter and large portions of |
| 16 | the Python run-time. Some measures have been taken to avoid linking |
| 17 | unneeded modules, but the resulting binary is usually not small. |
| 18 | |
| 19 | The Python source code of your program (and of the library modules |
| 20 | written in Python that it uses) is not included in the binary -- |
| 21 | instead, the compiled byte-code (the instruction stream used |
| 22 | internally by the interpreter) is incorporated. This gives some |
| 23 | protection of your Python source code, though not much -- a |
| 24 | disassembler for Python byte-code is available in the standard Python |
| 25 | library. At least someone running "strings" on your binary won't see |
| 26 | the source. |
| 27 | |
| 28 | |
| 29 | How does Freeze know which modules to include? |
| 30 | ---------------------------------------------- |
| 31 | |
| 32 | Freeze uses a pretty simple-minded algorithm to find the modules that |
| 33 | your program uses: given a file containing Python source code, it |
| 34 | scans for lines beginning with the word "import" or "from" (possibly |
| 35 | preceded by whitespace) and then it knows where to find the module |
| 36 | name(s) in those lines. It then recursively scans the source for |
| 37 | those modules (if found, and not already processed) in the same way. |
| 38 | |
| 39 | Freeze will not see import statements hidden behind another statement, |
| 40 | like this: |
| 41 | |
| 42 | if some_test: import M # M not seen |
| 43 | |
| 44 | or like this: |
| 45 | |
| 46 | import A; import B; import C # B and C not seen |
| 47 | |
| 48 | nor will it see import statements constructed using string |
| 49 | operations and passed to 'exec', like this: |
| 50 | |
| 51 | exec "import %s" % "M" # M not seen |
| 52 | |
| 53 | On the other hand, Freeze will think you are importing a module even |
| 54 | if the import statement it sees will never be executed, like this: |
| 55 | |
| 56 | if 0: |
| 57 | import M # M is seen |
| 58 | |
| 59 | One tricky issue: Freeze assumes that the Python interpreter and |
| 60 | environment you're using to run Freeze is the same one that would be |
| 61 | used to run your program, which should also be the same whose sources |
| 62 | and installed files you will learn about in the next section. In |
| 63 | particular, your PYTHONPATH setting should be the same as for running |
| 64 | your program locally. (Tip: if the program doesn't run when you type |
| 65 | "python hello.py" there's little chance of getting the frozen version |
| 66 | to run.) |
| 67 | |
| 68 | |
| 69 | How do I use Freeze? |
| 70 | -------------------- |
| 71 | |
Guido van Rossum | 96c4dd9 | 1996-08-26 05:14:20 +0000 | [diff] [blame] | 72 | Normally, you should be able to use it as follows: |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 73 | |
| 74 | python freeze.py hello.py |
| 75 | |
| 76 | where hello.py is your program and freeze.py is the main file of |
| 77 | Freeze (in actuality, you'll probably specify an absolute pathname |
Guido van Rossum | 96c4dd9 | 1996-08-26 05:14:20 +0000 | [diff] [blame] | 78 | such as /usr/joe/python/Tools/freeze/freeze.py). |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | 96c4dd9 | 1996-08-26 05:14:20 +0000 | [diff] [blame] | 80 | (With Python 1.4, freeze is much more likely to work "out of the box" |
| 81 | than before, provided Python has been installed properly.) |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 82 | |
| 83 | |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 84 | What do I do next? |
| 85 | ------------------ |
| 86 | |
| 87 | Freeze creates three files: frozen.c, config.c and Makefile. To |
| 88 | produce the frozen version of your program, you can simply type |
| 89 | "make". This should produce a binary file. If the filename argument |
Guido van Rossum | 96c4dd9 | 1996-08-26 05:14:20 +0000 | [diff] [blame] | 90 | to Freeze was "hello.py", the binary will be called "hello". |
| 91 | |
| 92 | Note: you can use the -o option to freeze to specify an alternative |
| 93 | directory where these files are created. This makes it easier to |
| 94 | clean up after you've shipped the frozen binary. |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 95 | |
| 96 | |
Guido van Rossum | bf6bdb0 | 1995-04-05 10:59:20 +0000 | [diff] [blame] | 97 | Troubleshooting |
| 98 | --------------- |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 99 | |
Guido van Rossum | bf6bdb0 | 1995-04-05 10:59:20 +0000 | [diff] [blame] | 100 | If you have trouble using Freeze for a large program, it's probably |
Guido van Rossum | 96c4dd9 | 1996-08-26 05:14:20 +0000 | [diff] [blame] | 101 | best to start playing with a really simple program first (like the file |
| 102 | hello.py). If you can't get that to work there's something |
| 103 | fundamentally wrong -- perhaps you haven't installed Python. To do a |
| 104 | proper install, you should do "make install" in the Python root |
| 105 | directory. |
Guido van Rossum | bf6bdb0 | 1995-04-05 10:59:20 +0000 | [diff] [blame] | 106 | |
| 107 | |
Guido van Rossum | 96c4dd9 | 1996-08-26 05:14:20 +0000 | [diff] [blame] | 108 | --Guido van Rossum (home page: http://www.python.org/~guido/) |