blob: 9aebb77282ff945ff112c8daa6c839bcf6b05088 [file] [log] [blame]
Jack Jansena2139fe1998-02-25 15:40:35 +00001<HTML><HEAD><TITLE>Creating true standalone applications in Python</TITLE></HEAD>
2<BODY>
3<H1>Creating true standalone applications in Python</H1>
4<HR>
Jack Jansend9690611998-06-26 15:01:52 +00005<EM>NOTE</EM> This document is obsolete. See <A HREF="freezing.html">Freezing Python
6scripts</A> for a more up-to-date treatise. <p>
7</HR>
Jack Jansena2139fe1998-02-25 15:40:35 +00008You can use Python to create true standalone macintosh applications: applications
9that you can distribute to other people as a single file, without dependencies
10on Python being installed, etc. The process is not easy, however, and at the
11moment you need a source distribution (and a C development environment, CodeWarrior
12most preferred). You should first familiarize yourself with the sections
13<a href="building.html">building Python from source</a> and
14<a href="example2.html">building applets</a>. <p>
15
16The application we are going to build will contain a complete interpreter,
17plus <code>'PYC '</code> resources for all the Python modules the program uses.
18We start by creating a resource file that contains all the modules we need,
19in PYC-resource form. There are two ways to do this:
20<UL>
21<LI> Modify the standard <code>freeze.py</code> module to print the names of
22all modules used. Copy these to a single folder, run <code>compileall.py</code>
23on that folder and then run <code>PackLibDir.py</code> from the scripts folder
24to create the resourcefile. This has one disadvantage: freeze finds the modules
25used by parsing your Python code, so modules you don't use (for instance because
26they are system-dependent and not used on the mac) are also included. You
27may also have problems with dynamically loaded modules. You will also have to rename
28your main module to __main__.py.
29
30<LI> Another way to find the modules used is by option-starting your script
31and setting the "interactive mode after script" flag. Exercise every corner of
32your program so all your modules have been imported, and when you exit your
33program and get back to the interpreter use <code>findmodulefiles.findmodulefiles</code>
34to get a list of all modules used. You can now use
35<code>findmodulefiles.mkpycresourcefile</code> to create your resourcefile.
36</UL>
37
38Next we create the application project. Copy the <code>PythonStandalone.prj</code>
39project, replace <code>macapplication.c</code> by <code>macapplet.c</code> and
40replace <code>bundle.rsrc</code> by <code>appletbundle.rsrc</code>. Also
41add the PYC resource file you made in the previous step and any other resource
42files you need. Set the target output file names (for all three of ppc/68k/fat).
43Build your application. <p>
44
45Finally we have to give the application the right <code>sys.path</code> initialisation.
46We do this by dropping the application on <code>EditPythonPrefs</code> and removing
47all path components replacing them with a single <code>$(APPLICATION)</code>. You
48may have to use ResEdit after this step to remove an "alis" resource from your application,
49I am not sure why this is sometimes created. <p>
50
51If you want to get fancy you may be able to make your application smaller by removing
52all unused builtin modules. If you used the findmodulefiles method above to find
53your modules you can start a standalone interpreter and use
54<code>findmodulefiles.findunusedbuiltins</code> to get the names of all builtin
55modules your program doesn't use. You can then create a private copy of
56<code>config.c</code> from which you remove all unused modules.
57
58</BODY></HTML>