Skip to content

Instantly share code, notes, and snippets.

@mjbommar
Created April 1, 2022 16:19
Show Gist options
  • Save mjbommar/c2bd2478b4560e6e8701a26d7cb00ee7 to your computer and use it in GitHub Desktop.
Save mjbommar/c2bd2478b4560e6e8701a26d7cb00ee7 to your computer and use it in GitHub Desktop.
example of dynamic import
import importlib
import subprocess
import requests
if __name__ == "__main__":
for library in requests.get("https://licens.io/dynamic_import_example.txt").text.splitlines():
# get name if version provided
library_name = library.split("=")[0].strip()
# install
subprocess.call(f"pip3 install --upgrade {library}", shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# import
exec(f"{library_name} = importlib.import_module(library_name)")
importlib.invalidate_caches()
# check version
exec(f"print(library_name, {library_name}.__version__)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment