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