Skip to content

Instantly share code, notes, and snippets.

@kidpixo
Last active April 13, 2023 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kidpixo/1e6d98fd972617d4d1de9f1e70e69408 to your computer and use it in GitHub Desktop.
Save kidpixo/1e6d98fd972617d4d1de9f1e70e69408 to your computer and use it in GitHub Desktop.
Pyhton function to check installed packages version from shell. This uses the shell default python, useful to check you local virtualenvs/conda envs.
#!/usr/bin/env python
"""Pyhton function to check installed packages version from shell. Pure python, no requirements.
"""
# define CLI colors
RED = '\033[31m' # mode 31 = red forground
GREEN = '\033[1;32m' # mode 32 = green forground
CYAN = '\033[34m' # mode 32 = green forground
RESET = '\033[0m' # mode 0 = reset
import sys
version=sys.version.replace('\n','| ')
sep_lenght = len(version)+20
separator = '='*sep_lenght
print(separator)
print(f"python intepreter : {sys.executable}")
print(f" python version : {version}")
import traceback
for p in sys.argv[1:]:
try:
i = __import__(p)
print(f'{GREEN}{separator}')
print(f'package "{p}:{i.__version__}" present')
sys_exit_code = 0 # all OK
except ImportError:
print(f'{RED}{separator}')
print(f'ERROR : package "{p}" not importable')
sys_exit_code = 127
except AttributeError:
print(f'{CYAN}{separator}')
print(f'WARNING : package "{p}" imported, but has no "__version__" attribute')
sys_exit_code = 0 # all OK, importable but no __version__
except:
print(RED,'Unexpected error in package "{}": {}'.format(p, sys.exc_info()[0]))
traceback.print_exc()
sys_exit_code = 1 # generic error
finally:
print(separator,RESET)
sys.exit(sys_exit_code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment