Skip to content

Instantly share code, notes, and snippets.

@jimbaker
Last active December 25, 2015 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimbaker/7035646 to your computer and use it in GitHub Desktop.
Save jimbaker/7035646 to your computer and use it in GitHub Desktop.
Using pkg_resources for data files packaged into a singlejar
>>> from java.io import BufferedInputStream as BIS
>>> import pkg_resources
>>> bis = BIS(pkg_resources.resource_stream("clamped", "data/example.txt"))
TypeError: java.io.BufferedInputStream(): 1st arg can't be coerced to java.io.InputStream
>>> f = pkg_resources.resource_stream("clamped", "data/example.txt")
>>> f.read()
'Some example text\n'
>>> path = pkg_resources.resource_filename("clamped", "data/example.txt")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/jbaker/opensource/clamped/clamped-0.1-single.jar/Lib/pkg_resources$py.class", line 944, in resource_filename
File "/Users/jbaker/opensource/clamped/clamped-0.1-single.jar/Lib/pkg_resources$py.class", line 1625, in get_resource_filename
NotImplementedError: resource_filename() only supported for .egg, not .zip
>>> pkg_resources.resource_string("clamped", "data/example.txt")
'Some example text\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment