import importlib import subprocess import sys VERSION = (1,0,0) def import_or_install(name, pypi_name=None): if pypi_name is None: pypi_name = name try: return importlib.import_module(name) except ImportError: try: subprocess.check_call([sys.executable, '-m', 'pip', 'install', '--upgrade', pypi_name]) except subprocess.CalledProcessError: subprocess.check_call([sys.executable, '-m', 'ensurepip']) subprocess.check_call([sys.executable, '-m', 'pip', 'install', '--upgrade', pypi_name]) return importlib.import_module(name)