python: can executable zip files include data files?

Posted on

Question :

python: can executable zip files include data files?

Being fairly new to python I only recently discovered the ability to directly execute a .zip file by placing a file at the top of the file. This works great for python code, but can I bundle other types of files and access them with my scripts? If so, how?

My ultimate goal would be to bundle some image files along with the python code in a single .zip file, then be able to use those images within the app without having to extract them to disk. I also would like to bundle a copyright notice, release notes, etc so that the entire app and its data files is in a single zip that can be executed without having to extract it somewhere.

Answer #1:

You could use pkg_resources functions to access files:

import pkg_resources
from PIL import Image

print pkg_resources.resource_string(__name__, 'README.txt')

im ='app', 'im.png'))

Where zipfile contains:

|-- app
|   |-- im.png
|   `--
|-- README.txt

To make zipfile executable, run:

$ echo '#!/usr/bin/env python' | cat - zipfile > program-name
$ chmod +x program-name

To test it:

$ cp program-name /another-dir/
$ cd /another-dir && ./program-name
Answered By: jfs

Answer #2:

simply use pkgutil.get_data() as suggested in this answer

Answered By: Xavier Combelle

Answer #3:

At least on my Linux box there is no open filehandle or mapped memory by the process to its own zipfile, so presumably there is no way to “magically” access it.

However, creating your own access is not that hard. Create a like so:

import os, zipfile

me = zipfile.ZipFile(os.path.dirname(__file__), 'r')
f ='other.txt')

Edit: Somewhat terse, that. For completeness:

$ echo "Hello ZIP" > other.txt
$ zip other.txt
$ python
Hello ZIP
Answered By: Bittrance

Answer #4:

pkgutil.get_data(package, resource) takes the name of a package and a resource. That means you have to put your data files inside a package within the zip file.

So for example a zip file containing:

The file can be empty or just a comment, but you need one to make zippeddata importable.

Then in you just call:

data = pkgutil.get_data('zippeddata', 'data.txt')
Answered By: Duncan

Leave a Reply

Your email address will not be published. Required fields are marked *