Jack Jansen | d969061 | 1998-06-26 15:01:52 +0000 | [diff] [blame^] | 1 | <HTML> |
| 2 | <HEAD> |
| 3 | <TITLE>Creating standalone applications with Python</TITLE> |
| 4 | </HEAD> |
| 5 | <BODY> |
| 6 | <H1>Creating standalone applications with Python</H1> |
| 7 | <HR> |
| 8 | With the <EM>macfreeze</EM> script you can <i>freeze</i> a Python |
| 9 | script: create a fullblown Macintosh application that is completely |
| 10 | self-contained. A frozen application is similar to an applet (see <a |
| 11 | href="example2.html">Example 2</a> for information on creating applets), |
| 12 | but where an applet depends on an existing Python installation for its |
| 13 | standard modules and interpreter core, a frozen program does not, |
| 14 | because it incorporates everything in a single binary. This means you |
| 15 | can copy a frozen program to a machine that does not have Python |
| 16 | installed and it will work, which is not true for an applet. <p> |
| 17 | |
| 18 | There are two ways to create a frozen application: through the |
| 19 | CodeWarrior development environment or without any development |
| 20 | environment. The former method is more versatile and may result in |
| 21 | smaller binaries, because you can better customize what is included in |
| 22 | your eventual application. The latter method builds an application by |
| 23 | glueing together the various <em>.slb</em> shared libraries that come |
| 24 | with a binary Python installation into a single file. This method of |
| 25 | freezing, which does not require you to spend money on a development |
| 26 | environment, is unique to MacPython, incidentally, on other platforms |
| 27 | you will always need a C compiler and linker. <p> |
| 28 | |
| 29 | <h2>Common steps</h2> |
| 30 | |
| 31 | The two processes have a number of steps in common. When you start |
| 32 | <code>Mac:Tools:macfreeze:macfreeze.py</code> you are asked for the |
| 33 | script file, and you can select which type of freeze to do. The first |
| 34 | time you should always choose <em>report only</em>, which will produce a |
| 35 | listing of modules and where they are included from in the console |
| 36 | window. Macfreeze actually parses all modules, so it may crash in the |
| 37 | process. If it does try again with a higher debug value, this should |
| 38 | show you where it crashes. <p> |
| 39 | |
| 40 | For more elaborate programs you will often see that freeze includes |
| 41 | modules you don't need (because they are for a different platform, for |
| 42 | instance) or that it cannot find all your modules (because you modify |
| 43 | <code>sys.path</code> early in your initialization). It is possible to |
| 44 | include directives to tell macfreeze to add items to the search path and |
| 45 | include or exclude certain modules. All your directives should be in the |
| 46 | main script file. <p> |
| 47 | |
| 48 | Directives have the following form: |
| 49 | <pre> |
| 50 | # macfreeze: command argument |
| 51 | </pre> |
| 52 | The trigger <code>macfreeze:</code> must be spelled exactly like that, |
| 53 | but the whitespace can be any combination of spaces and tabs. Macfreeze |
| 54 | understands the following directives: |
| 55 | |
| 56 | <DL> |
| 57 | <DT> <code>path</code> |
| 58 | <DD> Prepend a folder to <code>sys.path</code>. The argument is a |
| 59 | pathname, which should probably be relative (starting with a colon) and |
| 60 | is interpreted relative to the folder where the script lives. |
| 61 | |
| 62 | <DT> <code>include</code> |
| 63 | <DD> Include a module. The module can either be given by filename or by |
| 64 | module name, in which case it is looked up through the normal method. |
| 65 | |
| 66 | <DT> <code>exclude</code> |
| 67 | <DD> Exclude a module. The module must be given by modulename. Even when |
| 68 | freeze deems the module necessary it will not be included in the |
| 69 | application. |
| 70 | |
| 71 | </DL> |
| 72 | |
| 73 | There is actually a fourth way that macfreeze can operate: it can be used |
| 74 | to generate only the resource file containing the compiled <code>PYC</code> |
| 75 | resources. This may be useful if you have embedded Python in your own |
| 76 | application. The resource file generated is the same as for the CodeWarrior |
| 77 | generation process. <p> |
| 78 | |
| 79 | <h2>Freezing with CodeWarrior</h2> |
| 80 | |
| 81 | To freeze with CodeWarrior you need CodeWarrior, obviously, and a full |
| 82 | source distribution of Python. You select the <em>Codewarrior source and |
| 83 | project</em> option. You specify an output folder, which is by default |
| 84 | the name of your script with <code>.py</code> removed and |
| 85 | <code>build.</code> prepended. If the output folder does not exist yet |
| 86 | it is created, and a template project file and bundle resource file are |
| 87 | deposited there. Next, a source file <code>macfreezeconfig.c</code> is |
| 88 | created which includes all builtin modules your script uses, and a |
| 89 | resource file <code>frozenmodules.rsrc</code> which contains the |
| 90 | <code>PYC</code> resources for all your Python modules. <p> |
| 91 | |
| 92 | The project expects to live in a folder one level below the Python root |
| 93 | folder, so the next thing you should do is move the build folder there. |
| 94 | It is a good idea to leave an alias with the same name in the original |
| 95 | location: when you run freeze again it will regenerate the |
| 96 | <code>frozenmodules.rsrc</code> file but not the project and bundle |
| 97 | files. This is probably what you want: if you modify your python sources |
| 98 | you have to re-freeze, but you may have changed the project and bundle |
| 99 | files, so you don't want to regenrate them. <p> |
| 100 | |
| 101 | An alternative is to leave the build folder where it is, but then you |
| 102 | have to adapt the search path in the project. <p> |
| 103 | |
| 104 | The project is set up to include all the standard builtin modules, but |
| 105 | the CW linker is smart enough to exclude any object code that isn't |
| 106 | referenced. Still, it may be worthwhile to remove any sources for |
| 107 | modules that you are sure are not used to cut back on compilation time. |
| 108 | You may also want to examine the various resource files (for Tcl/Tk, for |
| 109 | instance): the loader has no way to know that these aren't used. <p> |
| 110 | |
| 111 | You may also need to add sourcefiles if your script uses non-standard |
| 112 | builtin modules, like anything from the <code>Extensions</code> folder. <p> |
| 113 | |
| 114 | The <code>frozenbundle.rsrc</code> resource file contains the bundle |
| 115 | information. It is almost identical to the bundle file used for applets, |
| 116 | with the exception that it sets the <code>sys.path</code> initialization |
| 117 | to <code>$(APPLICATION)</code> only. This means that all modules will only |
| 118 | be looked for in PYC resources in your application. <p> |
| 119 | |
| 120 | <h2>Freezing without CodeWarrior</h2> |
| 121 | |
| 122 | This does not work yet. |
| 123 | </BODY> |
| 124 | </HTML> |