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

  • Thread starter Brian Allen Vanderburg II
  • Start date
B

Brian Allen Vanderburg II

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
 
B

Brian Allen Vanderburg II

Why not use pkgutil.get_data()?

Provided you remember to put your zip file on PYTHONPATH you can already
run modules directly out of a zipfile (Python 2.5 and later).If your
zipfile contains __main__.py then with Python 2.6 or later you can run it
directly: just specify the zip filename on the command line.

I'm not sure what, if anything, you are asking for that doesn't already
exist in Python.

I wasn't aware of __main__.py for Python 2.6, I think this will work for
my needs. I'll look into using pkgutil for for loading data from the
archive.

Thanks.

Brian Vanderburg II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top