Skip to content

Instantly share code, notes, and snippets.

@mgbckr
Last active August 15, 2019 19:04
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 mgbckr/06586748c8ffd6357928e5bc1f1cd80a to your computer and use it in GitHub Desktop.
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
# 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