Created
April 1, 2022 16:19
-
-
Save mjbommar/c2bd2478b4560e6e8701a26d7cb00ee7 to your computer and use it in GitHub Desktop.
example of dynamic import
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
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