Created
July 21, 2023 09:51
-
-
Save f-steff/809786c5753bf0c36c3157a045964563 to your computer and use it in GitHub Desktop.
Install Python package at runtime
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 pkg_resources | |
import subprocess | |
import sys | |
import importlib | |
import pprint | |
def install(package): | |
print(f'\n=======> Checking status of {package}:', flush=True) | |
try: | |
dist = pkg_resources.get_distribution(package) | |
print('{} ({}) is installed'.format(dist.key, dist.version), flush=True) | |
except pkg_resources.DistributionNotFound: | |
print('{} is NOT installed'.format(package), flush=True) | |
subprocess.check_call([sys.executable, "-m", "pip", "install", package]) | |
# After installation, import the module and print its version | |
try: | |
pkg = importlib.import_module(package) | |
print(f'{package} ({pkg.__version__}) is installed') | |
except ImportError: | |
print(f"Failed to import {package}") | |
print (f"\n\n\n=========================================\nReport versions, and install Python packages, if needed.\n", flush=True) | |
install('flask') | |
install('gunicorn') | |
install('docker') | |
print (f"\n\nPackages are ready for use.\n=========================================\n\n", flush=True) | |
# Now that we have ensured Flask is installed, we can import it and run our main program | |
from flask import Flask, request, Response, redirect, render_template |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment