Attempt to return the filename of the currently running Python process

    Returns None if the filename cannot be detected.

def detect_current_filename():
    ''' Attempt to return the filename of the currently running Python process

    Returns None if the filename cannot be detected.
    '''
    import inspect

    filename = None
    frame = inspect.currentframe()
    try:
        while frame.f_back and frame.f_globals.get('name') != '__main__':
            frame = frame.f_back

        filename = frame.f_globals.get('__file__')
    finally:
        del frame

    return filename