import pkgutil
pkgutil.get_data(<module name>, 'resource1.txt')
will return a bytes object.
<module name>
should be a string containing the path to the folder where resource1.txt is, as though you were trying to import it; e.g. if wanted /res/some_subfolder/foo.txt
then you would do pkgutil.get_data('yourmodule.res.some_subfolder', 'foo.txt')
.
Note that res
and some_subfolder
in this instance must be full on Python modules, not just directories (the difference being the presence or absence of an empty file named __init__.py
); otherwise zipapp will ignore them. Your folder structure in in the above example must therefore be:
project root
| main.py
| <whatever other files you have>
| res
| | __init__.py
| | some_subfolder
| | | __init__.py
| | | foo.txt
Again, these files can be blank if you wish, they just have to exist (although if you do put code in them, you can then do import res
(or import res.some_subfolder
) and it will run res/__init__.py
, which could be a useful way to organize all of your resoures from one place if you feel like doing it that way). By the way, if anyone reading this is, like the majority of Python programmers, unfamiliar with how modules work, here's a quick tutorial from the Python docs.
Additionally, if you are just using zipapp (as opposed to an exe created with pyinstaller) you can simply:
import sys
import zipfile
with zipfile.ZipFile(sys.argv[0]) as zf:
with zf.open('res/resource1.txt') as f:
print(f.read())