Distributing Python apps

cmkl cmkleffner at gmx.de
Tue Jan 7 08:40:18 CET 2003


Just van Rossum <just at letterror.com> wrote in message news:<mailman.1041879011.21003.python-list at python.org>...
> Bjorn Pettersen wrote:
> 
> > [ ... ] It seems it might be useful to add a module to the library
> > that emits the graph either through static analysis, or by logging
> > imports during a run. Static analysis could probably catch 95% of the
> > imports, and even with run-time logging you might be hosed if you
> > don't execute the program path that imports a module. If you combine
> > them I would think you'd get in the 99% range (which I think is quite
> > acceptable for a language like Python).
> 
> Python 2.3a1 contains modulefinder.py (used to be part of the Freeze
> package). It does static import analysis and keeps track of which
> modules import which. It still needs to be documented, though... It only
> misses imports done in exec statements and by __import__ calls.
> 
> Just

building a single file python runtime is far easy on Unix systems.
Take cgipython or cxfreeze as example.

I built my own single file interpreter with freeze some months ago 
with following capabilities:

 - interpreter can be used interacivly (enhanced by pyrepl-0.6)
 - interpreter can start python scripts alternativly
 - import path is modified to use modules in the script directory.

The single file 'pyrt' can be deployed i.e. as 'python'
to the customer. The programs can be deployed as python scripts 
or bytecode (if not freezed within the binary).
This should work on Unix systems. 
I will try to make this work on windows with the free 
mingw gcc compiler.

Sincerly

Carl Kleffner


--------------------------------------------------------------------------
#!/usr/bin/env python

# freeze Python with some modules
#
# Not freezed in this example:
#   thread module
#   socket module
#   Tkinter module
#   unicode, xml

import sys,os

if [0][0]:                                # fool the modulefinder

    # modify this to your needs!

    import __future__, warnings

    #warnings.filterwarnings("ignore")
    import regex                          # import w/o deprec. warning
    #warnings.resetwarnings()

    import time, timing
    import pyrepl.python_reader, pyrepl_utils # if you have pyrepl
    import strop, struct, copy_reg, cStringIO, cPickle
    import array, bisect, operator, difflib
    import cmd, commands, curses
    import getopt, glob, termios, profile, pstats
    import shmem, locale, linecache
    import inspect, pydoc
    import math, cmath
    import filecmp, fileinput, fnmatch
    import re, types, tempfile
    import shelve, shlex, shutil
    import weakref, random, whrandom
    import pipes, pprint, fpformat
    import base64, Bastion, binascii, calendar
    import errno, atexit, fcntl, glob, fpectl
    import gzip, zlib, zipfile, dircache
    import marshal, mmap, new, codecs
    import posix, rexec, rotor, select, signal
    import UserDict, UserList, UserString
    import xreadlines, csv, crypt, exceptions

#--- modify sys.path und sys.argv[0]

sys.argv = sys.argv[1:]         # del first argument
root_dir = sys.exec_prefix      # root path for module
sys_path = sys.path[:]

for i in sys_path:              # del root path from syspath
    if i.find(root_dir) >= 0:
        idx = sys.path.index(i)
        sys.path.pop(idx)

del root_dir, sys_path
if 'i' in vars():   del i
if 'idx' in vars(): del idx

if sys.argv and os.path.isfile( sys.argv[0] ): # script exists?

    script_dirname = os.path.dirname(
        os.path.abspath( sys.argv[0] ))        # absolute dir

    if script_dirname not in sys.path:
        sys.path.append( script_dirname )
    del script_dirname

    try:
        execfile( os.path.abspath( sys.argv[0] ) ) # run script
    except:
        print "\nruntime error with: %s!" % os.path.abspath(
sys.argv[0] )

else:                                      # python interactive

    print "single file python interpreter."
    print "usage:  python [ skriptfile [ args ] ]\n"
    print "hint:   put '#!/usr/bin/env python' as first line"
    print "        in your executable python script.\n"
    print "Python %s on %s" % (sys.version, sys.platform)
    print 'Type "copyright", "credits" or "license" for more
information.'
    print 'Hit <CTRL-D> to exit and <CTRL-N> <CTRL-P> to scroll
history.'

    try:
        sys.path.append( os.path.abspath(os.curdir) )
        from   pyrepl.python_reader import ReaderConsole
        from   pydoc                import help
        exec( "ReaderConsole(vars(), 0, None).interact()" )
    except:
        pass

# freeze that with: 
# python /yourpath2python/Python-2.2/Tools/freeze/freeze.py -o pyrt -X
macpath -X ntpath -X dospath  -X threading pyrt.py




More information about the Python-list mailing list