def main_is_frozen():
    return hasattr(sys, 'frozen') or hasattr(sys, 'importers'
        ) or imp.is_frozen('__main__')

def get_main_dir():
    if main_is_frozen():
        return os.path.dirname(sys.executable)
return os.path.dirname(sys.argv[0])
path_to_script = get_main_dir()
lib_path = os.path.join(get_main_dir(), os.path.pardir, 'lib')
sys.path.insert(0, lib_path)
datafile1 = os.path.join(get_main_dir(), 'data', 'file1')
How to get filename of the __main__ module in Python?