Created
June 9, 2019 21:42
-
-
Save 4goettma/f5d4b6f7801ca82fcfc762efb8c1b95b to your computer and use it in GitHub Desktop.
small python module to simplify importing modules, v0.2
This file contains hidden or 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
""" | |
#!/usr/bin/env python3 | |
import importHelper | |
importHelper.importAllOfThese(["datetime", "math"]) | |
importHelper.importOneOfThese(["clipboard", "pyperclip"], "clip") | |
print(math.cos(1)) | |
print(datetime.datetime.now().isoformat()) | |
print(clip.paste()) | |
""" | |
import importlib, inspect | |
VERSION = "0.2" | |
def importAllOfThese(modules): | |
missing = [] | |
for module in modules: | |
try: | |
inspect.stack()[1][0].f_globals[module]=importlib.import_module(module) | |
except ImportError: | |
missing.append(module) | |
if (missing): | |
print("\nPlease install missing dependencies:\nsudo python3 -m pip install "+"".join(missing)+"\n") | |
exit(1) | |
return | |
def importOneOfThese(modules, handle): | |
if (not modules): | |
return None | |
for module in modules: | |
try: | |
inspect.stack()[1][0].f_globals[handle]=importlib.import_module(module) | |
return | |
except ImportError: | |
pass | |
print("\nPlease install missing dependencies ("+" or ".join(modules)+"):") | |
for module in modules: | |
print("sudo python3 -m pip install "+module) | |
exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment