Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 1 | THE FREEZE SCRIPT |
| 2 | ================= |
| 3 | |
Guido van Rossum | 7ba3de4 | 1997-08-14 02:12:04 +0000 | [diff] [blame] | 4 | (Directions for Windows NT are at the end of this file.) |
| 5 | |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 6 | |
| 7 | What is Freeze? |
| 8 | --------------- |
| 9 | |
| 10 | Freeze make it possible to ship arbitrary Python programs to people |
| 11 | who don't have Python. The shipped file (called a "frozen" version of |
| 12 | your Python program) is an executable, so this only works if your |
| 13 | platform is compatible with that on the receiving end (this is usually |
| 14 | a matter of having the same major operating system revision and CPU |
| 15 | type). |
| 16 | |
| 17 | The shipped file contains a Python interpreter and large portions of |
| 18 | the Python run-time. Some measures have been taken to avoid linking |
| 19 | unneeded modules, but the resulting binary is usually not small. |
| 20 | |
| 21 | The Python source code of your program (and of the library modules |
| 22 | written in Python that it uses) is not included in the binary -- |
| 23 | instead, the compiled byte-code (the instruction stream used |
| 24 | internally by the interpreter) is incorporated. This gives some |
| 25 | protection of your Python source code, though not much -- a |
| 26 | disassembler for Python byte-code is available in the standard Python |
| 27 | library. At least someone running "strings" on your binary won't see |
| 28 | the source. |
| 29 | |
| 30 | |
| 31 | How does Freeze know which modules to include? |
| 32 | ---------------------------------------------- |
| 33 | |
| 34 | Freeze uses a pretty simple-minded algorithm to find the modules that |
| 35 | your program uses: given a file containing Python source code, it |
| 36 | scans for lines beginning with the word "import" or "from" (possibly |
| 37 | preceded by whitespace) and then it knows where to find the module |
| 38 | name(s) in those lines. It then recursively scans the source for |
| 39 | those modules (if found, and not already processed) in the same way. |
| 40 | |
| 41 | Freeze will not see import statements hidden behind another statement, |
| 42 | like this: |
| 43 | |
| 44 | if some_test: import M # M not seen |
| 45 | |
| 46 | or like this: |
| 47 | |
| 48 | import A; import B; import C # B and C not seen |
| 49 | |
| 50 | nor will it see import statements constructed using string |
| 51 | operations and passed to 'exec', like this: |
| 52 | |
| 53 | exec "import %s" % "M" # M not seen |
| 54 | |
| 55 | On the other hand, Freeze will think you are importing a module even |
| 56 | if the import statement it sees will never be executed, like this: |
| 57 | |
| 58 | if 0: |
| 59 | import M # M is seen |
| 60 | |
| 61 | One tricky issue: Freeze assumes that the Python interpreter and |
| 62 | environment you're using to run Freeze is the same one that would be |
| 63 | used to run your program, which should also be the same whose sources |
| 64 | and installed files you will learn about in the next section. In |
| 65 | particular, your PYTHONPATH setting should be the same as for running |
| 66 | your program locally. (Tip: if the program doesn't run when you type |
| 67 | "python hello.py" there's little chance of getting the frozen version |
| 68 | to run.) |
| 69 | |
| 70 | |
| 71 | How do I use Freeze? |
| 72 | -------------------- |
| 73 | |
Guido van Rossum | 96c4dd9 | 1996-08-26 05:14:20 +0000 | [diff] [blame] | 74 | Normally, you should be able to use it as follows: |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 75 | |
| 76 | python freeze.py hello.py |
| 77 | |
| 78 | where hello.py is your program and freeze.py is the main file of |
| 79 | Freeze (in actuality, you'll probably specify an absolute pathname |
Guido van Rossum | 96c4dd9 | 1996-08-26 05:14:20 +0000 | [diff] [blame] | 80 | such as /usr/joe/python/Tools/freeze/freeze.py). |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 83 | What do I do next? |
| 84 | ------------------ |
| 85 | |
| 86 | Freeze creates three files: frozen.c, config.c and Makefile. To |
| 87 | produce the frozen version of your program, you can simply type |
| 88 | "make". This should produce a binary file. If the filename argument |
Guido van Rossum | 96c4dd9 | 1996-08-26 05:14:20 +0000 | [diff] [blame] | 89 | to Freeze was "hello.py", the binary will be called "hello". |
| 90 | |
| 91 | Note: you can use the -o option to freeze to specify an alternative |
| 92 | directory where these files are created. This makes it easier to |
| 93 | clean up after you've shipped the frozen binary. |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 94 | |
| 95 | |
Guido van Rossum | bf6bdb0 | 1995-04-05 10:59:20 +0000 | [diff] [blame] | 96 | Troubleshooting |
| 97 | --------------- |
Guido van Rossum | d8336c2 | 1994-10-05 16:13:01 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | bf6bdb0 | 1995-04-05 10:59:20 +0000 | [diff] [blame] | 99 | 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] | 100 | best to start playing with a really simple program first (like the file |
| 101 | hello.py). If you can't get that to work there's something |
| 102 | fundamentally wrong -- perhaps you haven't installed Python. To do a |
| 103 | proper install, you should do "make install" in the Python root |
| 104 | directory. |
Guido van Rossum | bf6bdb0 | 1995-04-05 10:59:20 +0000 | [diff] [blame] | 105 | |
| 106 | |
Guido van Rossum | 7ba3de4 | 1997-08-14 02:12:04 +0000 | [diff] [blame] | 107 | Usage under Windows NT |
| 108 | ---------------------- |
| 109 | |
| 110 | Under Windows NT, you *must* use the -p option and point it to the top |
| 111 | of the Python source tree. |
| 112 | |
| 113 | WARNING: the resulting executable is not self-contained; it requires |
| 114 | the Python DLL, currently PYTHON15.DLL (it does not require the |
| 115 | standard library of .py files though). |
| 116 | |
| 117 | The driver script generates a Makefile that works with the Microsoft |
| 118 | command line C compiler (CL). To compile, run "nmake"; this will |
| 119 | build a target "hello.exe" if the source was "hello.py". Only the |
| 120 | files frozenmain.c and frozen.c are used; no config.c is generated or |
| 121 | used, since the standard DLL is used. |
| 122 | |
| 123 | In order for this to work, you must have built Python using the VC++ |
| 124 | (Developer Studio) 5.0 compiler. The provided project builds |
| 125 | python15.lib in the subdirectory pcbuild\Release of thje Python source |
| 126 | tree, and this is where the generated Makefile expects it to be. If |
| 127 | this is not the case, you can edit the Makefile or (probably better) |
| 128 | winmakemakefile.py (e.g., if you are using the 4.2 compiler, the |
| 129 | python15.lib file is generated in the subdirectory vc40 of the Python |
| 130 | source tree). |
| 131 | |
| 132 | Freezing pure GUI applications has not yet been tried; there's a new |
| 133 | -s option to specify the subsystem, but only the default ('console') |
| 134 | has been tested. Freezing applications using Tkinter works; note that |
| 135 | these will require that that _tkinter.dll is available and the right |
| 136 | version of Tcl/Tk (the one that was used to build _tkinter.dll) is |
| 137 | installed. |
| 138 | |
Guido van Rossum | 96c4dd9 | 1996-08-26 05:14:20 +0000 | [diff] [blame] | 139 | --Guido van Rossum (home page: http://www.python.org/~guido/) |