[Pythonmac-SIG] was : py2app: how to include the entire standard library of modules?

Nick Anderson an003141 at gmail.com
Wed Oct 3 23:59:34 CEST 2007


You wrote:
>hi Nick
>
>did you finally manage to include the entire standard library into an
>executable app? I am pretty interested to know about this, I had an idea
>where I needed to achieve this and I want to try again.
>
>thanks!
>
>enrike

I did find a solution, but it involves using another script to edit
the setup.py script created by py2applet. (If anyone knows of an
easier way to do this, let me know.) Make the following changes to
setup.py:

includes = []

OPTIONS = {'argv_emulation': True,
            'plist':plist,
            'includes':includes,

Next you will run the following script in the same folder as your
setup.py. Put the names of the packages you don't want included in the
skip list.

#Collects all standard modules with paths and inserts them into the config file
#for py2app.

import os
import os.path
import sys
path = '/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5'

modules = []
skip = ['site-packages', 'lib-tk', 'test', 'idlelib']

for root, dirs, files in os.walk(path):
    for item in skip:
        if item in dirs:
            dirs.remove(item)
    for f in files:
        d = root.replace(path, '')
        full_path = os.path.join(d, f)
        if '\\' in full_path:
            full_path = full_path.replace('\\', '.')
        if '/' in full_path:
            full_path = full_path.replace('/', '.')
        if full_path[0] == '.':
            full_path = full_path[1:]
        if full_path.startswith('plat-mac.'):
            full_path = full_path[9:]
        if full_path.startswith('lib-scriptpackages.'):
            full_path = full_path[18:]
        if full_path.startswith('plat-darwin.'):
            full_path = full_path[11:]
        if full_path.startswith('.'):
            continue
        #Ignore .pyc and .pyo files
        if f.endswith('.py') \
            and not f.startswith('_'):
            full_path = full_path[:-3]
            #Save each part of the file path as part of the module name:
            #foo.foo1.foo2.py has a package foo, a sub-package foo1,
            #and a module foo2. Save foo, foo.foo1, and foo.foo1.foo2.py.
            section_total = full_path.count('.')
            start = 0
            for x in range(section_total):
                stop = full_path.find('.', start)
                if stop != -1:
                    package = full_path[:stop]
                    if package and package not in modules:
                        modules.append(package)
                start = stop + 1
            if full_path and full_path not in modules:
                modules.append(full_path)

modules.remove('anydbm') #This module fails for some reason.

print 'The number of modules is', len(modules)

mac_file = open('setup.py', 'r')
lines = mac_file.readlines()
mac_file.close()

for x in range(len(lines)):
    if lines[x].startswith('includes ='):
        lines[x] = 'includes = ' + str(modules)
        break
mac_file = open('setup.py', 'w')
mac_file.writelines(lines)
mac_file.close()


More information about the Pythonmac-SIG mailing list