Last active
August 15, 2019 19:04
-
-
Save mgbckr/06586748c8ffd6357928e5bc1f1cd80a to your computer and use it in GitHub Desktop.
Access path of file in module (even if in zip file) in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Source: https://stackoverflow.com/a/20885799/991496 | |
# IMPORANT: There needs to be a `__init__.py` in `your.lib.resources` | |
# otherwise `pkg_resources.path` throws a FileNotFoundError saying "<resource> resource not found in <package_name>". | |
import your.lib.resources | |
try: | |
import importlib.resources as pkg_resources | |
except ImportError: | |
# Try backported to PY<37 `importlib_resources`. | |
import importlib_resources as pkg_resources | |
with pkg_resources.path(your.lib.resources, "load_fcs.R") as path: | |
with open(path, "rb") as f: | |
# do something |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment