Skip to content

Instantly share code, notes, and snippets.

@fherbine
Last active September 9, 2020 08:29
Show Gist options
  • Save fherbine/783d058a743732d9ddb413bf625383de to your computer and use it in GitHub Desktop.
Save fherbine/783d058a743732d9ddb413bf625383de to your computer and use it in GitHub Desktop.
Check if a module is whether standard or not + version checking
import sys
from importlib import import_module
from pip.commands.list import get_installed_distributions as get_modules
MODULES = get_modules()
if __name__ == '__main__':
module_name = sys.argv[-1]
tmp_path = sys.path
sys.path = []
std = True
version = 'undefined'
try:
module = import_module(module_name)
except ImportError:
std = False
module = None
except:
raise
sys.path = tmp_path
try:
module = import_module(module_name)
version = module.__version__
except AttributeError:
for mod in MODULES:
#print(mod)
if module_name in str(mod):
version = 'Should be: `%s`' % str(mod)
break
except ImportError:
print('...Module is probably not installed !')
sys.exit(-1)
print('[STD]: %s' % ('YES' if std else 'NO'))
print('[VERSION]: "%s"' % version)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment