This page looks best with JavaScript enabled

Accessing files from zip with package resources in python

 ·   ·  β˜• 1 min read

You could use pkg_resources functions to access files:

# __main__.py
import pkg_resources
from PIL import Image

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

im = Image.open(pkg_resources.resource_stream('app', 'im.png'))
im.rotate(45).show()

Where zipfile contains:

.
|-- app
|   |-- im.png
|   `-- __init__.py
|-- README.txt
`-- __main__.py

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

Ohidur Rahman Bappy
WRITTEN BY
Ohidur Rahman Bappy
πŸ“šLearner 🐍 Developer