Python idea/proposal to assist in single-archive python applications

Brian Allen Vanderburg II BrianVanderburg2 at aim.com
Sun Dec 7 10:31:21 EST 2008


Python Community

The following is just an idea that I considered that may be helpful in 
creating an application in a single archive easier and with less code.  
Such an application would be similar to jar files for Java.

First, the application and all data files should be able to run either 
extracted or zipped up into an archive.  When running an application as 
part of an archive, the first step Python would do would be to insert 
that archive into sys.path, and then load the internal file of the same 
name as the __main__ module. 

This zip file:

myapp.zip
 |
 |-- myapp.py
 |-- myapp.pyc (optional)
 |-- application/ (main application package)
 |-- pixmaps/
 |-- ...

Could be run as something like:

python --par myapp.zip

In addition it is needed to be able to open a file just as easily 
whether that file is in the archive or not.  Assuming that a datafile in 
an application may be located relative to the '__file__' attributes, the 
following will not work in an archive:

file = open(os.path.join(os.path.dirname('__file__'), '..', 'pixmaps', 
'splash.png'))

However, a simple function, perhaps built-in, could be provided which 
would work for reading either internal files or regular files.  That is, 
the function would be able to open a file '/path/to/file', and it would 
also be able to open a file '/path/to/zipfile.zip/internal/file'.  If 
built in the function could also take advantage of the open modes 'rb', 
'rt' and 'rU'.

Currently this is not so easy.  First it would require the user writing 
a function to be able to open a file internally and externally just as 
easily, using zipfile and StringIO for any internal files.  Secondly, 
opening a file in such a way only allows binary opens, it can't take 
advantage of pythons 'rU' open mode if the application so needs.  Also, 
it still requires an external startup script to set the path and import 
the internal module.

If this function was provided in Python, then and application could 
fairly easily run just as well zipped up as they would unzipped.

Brian Vanderburg II



More information about the Python-list mailing list