Idiom for running compiled python scripts?

Mark mark at mailinator.com
Sat Mar 24 19:46:15 EDT 2007


On Sat, 24 Mar 2007 07:21:21 -0700, irstas wrote:
> A simple implementation that "works":

Not quite irstas BTW ..

> import imp, sys, os
> c = sys.argv[1]
> if not os.path.exists(c + 'c') or os.stat(c).st_mtime > os.stat(c +
> 'c').st_mtime:
>     import compiler
>     compiler.compileFile(c)
> del sys.argv[0]
> imp.load_compiled('__main__', c + 'c')

The above doesn't actually work for my test script. I have an atexit
call in the script which is deleting some temp files and I get the
following traceback on termination when run with the above:

Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "/home/mark/bin/myscript.py", line 523, in delete
    if files.tempdir:
AttributeError: 'NoneType' object has no attribute 'tempdir'
Error in sys.exitfunc:
Traceback (most recent call last):
  File "/usr/lib/python2.4/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "/home/mark/bin/myscript.py", line 523, in delete
    if files.tempdir:
AttributeError: 'NoneType' object has no attribute 'tempdir'

The appropriate source code is:

At the start of main() ..

    # Ensure all temp files deleted on exit
    import atexit
    atexit.register(files.delete)

and then from my class "files":

    @staticmethod
    def delete():
	'''Called to delete all temp files'''

	if files.tempdir:
	    shutil.rmtree(files.tempdir)

Something about the environment is not quite the same. Any ideas?



More information about the Python-list mailing list