blob: 5097b7805be783640f07f4dadb2c94698b073c89 [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>
Jack Jansend9690611998-06-26 15:01:52 +00007
Jack Jansendd206891999-12-03 16:00:20 +00008With <a href="example2.html#applet">BuildApplet</a> you can build a standalone
9Python application that works like
10any other Mac application: you can double-click it, run it while the
11Python interpreter is running other scripts, drop files on it, etc. It is, however,
12still dependent on the whole Python installation on your machine: the PythonCore
13engine, the plugin modules and the various Lib folders.<p>
Jack Jansend9690611998-06-26 15:01:52 +000014
Jack Jansendd206891999-12-03 16:00:20 +000015In some cases you may want to create a true application, for instance because
16you want to send it off to people who may not have Python installed on their
17machine, or because you the application is important and you do not want changes
18in your Python installation like new versions to influence it.
Jack Jansend9690611998-06-26 15:01:52 +000019
Jack Jansendd206891999-12-03 16:00:20 +000020<H2>The easy way</H2>
21
22The easiest way to create an application from a Python script is simply by dropping
23it on the <code>BuildApplication</code> applet in the main Python folder.
24BuildApplication has a similar interface as BuildApplet: you drop a script on
25it and it will process it, along with an optional <code>.rsrc</code> file.
26It does ask one extra question: whether you want to build your application for
27PPC macs only, 68K macs or any Mac.<P>
28
29What BuildApplication does, however, is very different. It parses your script,
30recursively looking for all modules you use, bundles the compiled code for
31all these modules in PYC resources, adds the executable machine code for the
32PythonCore engine, any dynamically loaded modules you use and a main program, combines
33all this into a single file and adds a few preference resources (which you
34can inspect with <code>EditPythonPrefs</code>, incidentally) to isolate the
35new program from the existing Python installation.<P>
36
37Usually you do not need to worry about all this, but occasionally you may have
38to exercise some control over the process, for instance because your
39program imports modules that don't exist (which can happen if your script
40is multi-platform and those modules will never be used on the Mac). See
41the section on <a href="#directives">directives</a> below for details.
42If you get strange error messages about missing modules it may also be worthwhile
43to run macfreeze in report mode on your program, see below.
44<P>
45
46<H2>Doing it the hard way</H2>
47
48With the <EM>macfreeze</EM> script, for which BuildApplication is a simple
49wrapper, you can go a step further and create CodeWarrior projects and
50sourcefiles which can then be used to build your final application. While
51BuildApplication is good enough for 90% of the use cases there are situations
52where you need macfreeze itself, mainly if you want to embed your frozen Python
53script into an existing C application, or when you need the extra bit of speed:
54the resulting application will start up a bit quicker than one generated
55with BuildApplication. <p>
56
57When you start
Jack Jansend9690611998-06-26 15:01:52 +000058<code>Mac:Tools:macfreeze:macfreeze.py</code> you are asked for the
59script file, and you can select which type of freeze to do. The first
60time you should always choose <em>report only</em>, which will produce a
61listing of modules and where they are included from in the console
62window. Macfreeze actually parses all modules, so it may crash in the
63process. If it does try again with a higher debug value, this should
64show you where it crashes. <p>
65
Jack Jansendd206891999-12-03 16:00:20 +000066<h2><a name="directives">Directives</a></h2>
67
Jack Jansend9690611998-06-26 15:01:52 +000068For more elaborate programs you will often see that freeze includes
69modules you don't need (because they are for a different platform, for
70instance) or that it cannot find all your modules (because you modify
71<code>sys.path</code> early in your initialization). It is possible to
72include directives to tell macfreeze to add items to the search path and
73include or exclude certain modules. All your directives should be in the
74main script file. <p>
75
76Directives have the following form:
77<pre>
78# macfreeze: command argument
79</pre>
80The trigger <code>macfreeze:</code> must be spelled exactly like that,
81but the whitespace can be any combination of spaces and tabs. Macfreeze
82understands the following directives:
83
84<DL>
85<DT> <code>path</code>
86<DD> Prepend a folder to <code>sys.path</code>. The argument is a
87pathname, which should probably be relative (starting with a colon) and
88is interpreted relative to the folder where the script lives.
89
90<DT> <code>include</code>
91<DD> Include a module. The module can either be given by filename or by
92module name, in which case it is looked up through the normal method.
93
94<DT> <code>exclude</code>
95<DD> Exclude a module. The module must be given by modulename. Even when
96freeze deems the module necessary it will not be included in the
97application.
98
Jack Jansendd206891999-12-03 16:00:20 +000099<DT> <code>optional</code>
100<DD> Include a module if it can be found, but don't complain if it can't.
101
Jack Jansend9690611998-06-26 15:01:52 +0000102</DL>
103
104There is actually a fourth way that macfreeze can operate: it can be used
105to generate only the resource file containing the compiled <code>PYC</code>
106resources. This may be useful if you have embedded Python in your own
107application. The resource file generated is the same as for the CodeWarrior
108generation process. <p>
109
110<h2>Freezing with CodeWarrior</h2>
111
112To freeze with CodeWarrior you need CodeWarrior, obviously, and a full
113source distribution of Python. You select the <em>Codewarrior source and
114project</em> option. You specify an output folder, which is by default
115the name of your script with <code>.py</code> removed and
116<code>build.</code> prepended. If the output folder does not exist yet
117it is created, and a template project file and bundle resource file are
118deposited there. Next, a source file <code>macfreezeconfig.c</code> is
119created which includes all builtin modules your script uses, and a
120resource file <code>frozenmodules.rsrc</code> which contains the
121<code>PYC</code> resources for all your Python modules. <p>
122
123The project expects to live in a folder one level below the Python root
124folder, so the next thing you should do is move the build folder there.
125It is a good idea to leave an alias with the same name in the original
126location: when you run freeze again it will regenerate the
127<code>frozenmodules.rsrc</code> file but not the project and bundle
128files. This is probably what you want: if you modify your python sources
129you have to re-freeze, but you may have changed the project and bundle
Jack Jansendd206891999-12-03 16:00:20 +0000130files, so you don't want to regenerate them. <p>
Jack Jansend9690611998-06-26 15:01:52 +0000131
132An alternative is to leave the build folder where it is, but then you
133have to adapt the search path in the project. <p>
134
135The project is set up to include all the standard builtin modules, but
136the CW linker is smart enough to exclude any object code that isn't
137referenced. Still, it may be worthwhile to remove any sources for
138modules that you are sure are not used to cut back on compilation time.
139You may also want to examine the various resource files (for Tcl/Tk, for
140instance): the loader has no way to know that these aren't used. <p>
141
142You may also need to add sourcefiles if your script uses non-standard
143builtin modules, like anything from the <code>Extensions</code> folder. <p>
144
145The <code>frozenbundle.rsrc</code> resource file contains the bundle
146information. It is almost identical to the bundle file used for applets,
147with the exception that it sets the <code>sys.path</code> initialization
148to <code>$(APPLICATION)</code> only. This means that all modules will only
149be looked for in PYC resources in your application. <p>
150
Jack Jansend9690611998-06-26 15:01:52 +0000151</BODY>
152</HTML>