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