import py_compile
py_compile.compile('/path/to/valid/python/file.py')
py_compile.compile('/path/to/in-valid/python/file.txt')
How can I check on runtime that a python module is valid without importing it?