Skip to content

Instantly share code, notes, and snippets.

@scivision
Last active June 29, 2023 14:18
Show Gist options
  • Save scivision/f4f20aa824c45a04e30d430d580589f3 to your computer and use it in GitHub Desktop.
Save scivision/f4f20aa824c45a04e30d430d580589f3 to your computer and use it in GitHub Desktop.
importlib.resources for Python >= 3.9
#!/usr/bin/env python3
"""
Assumes executable "amender.bin" exists in the Python package and you want to resolve its full path and run it.
"""
import os
import subprocess
import importlib.resources as impr
def run_package_exe(name: str = "amender.bin"):
if os.name == "nt":
name += ".exe"
if not impr.as_file(impr.files(__package__).joinpath(name)).is_file():
raise ImportError(f"{name} not found, please see README")
with impr.as_file(impr.files(__package__).joinpath(name)) as exe:
subprocess.check_call([str(exe), myparam1, myparam2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment